活动地址:CSDN21天学习挑战赛
🍁博客主页:
👉@不会压弯的小飞侠
✨欢迎关注:
👉点赞
👍收藏
⭐留言
✒
✨系列专栏:
👉21天挑战回顾java
✨知足上进,不负野心。
🔥欢迎大佬指正,一起学习!一起加油!
加入社区领红包!http://t.csdn.cn/wcuEQ
加入社区领红包!http://t.csdn.cn/pA5RN
🔥第二周学习内容:面向对象基础
学习内容:
🔥Java面向对象编程三大特征-多态
格式:父类 变量名 = new 子类();
案例分析:
public class Test {
public void test(Person person) {
person.study();
person.sleep();
}
public static void main(String[] args) {
Test t= new Test ();
t.test(new Student());
System.out.println("---------");
t.test(new Teacher());
}
}
class Person{
public void study(){
System.out.println("study...");
}
public void sleep(){
System.out.println("sleep...");
}
}
class Student extends Person{
public void study(){
System.out.println("student study...");
}
public void sleep(){
System.out.println("student sleep...");
}
}
class Teacher extends Person{
public void study(){
System.out.println("teacher study...");
}
public void sleep(){
System.out.println("teacher sleep...");
}
}
父类类型 变量名 = new 子类类型();
子类类型 变量名 = (子类类型)父类类型的变量;
案例分析:
public class Test {
public static void main(String[] args) {
Person person = new Student();
Student student = new Student(); //向上转型
Student student1 = (Student)person; //向下转型
student.sleep();
student1.sleep();
}
}
class Person{
public void sleep() {
System.out.println("sleep...");
}
}
class Student extends Person{
public void sleep() {
System.out.println("student sleep...");
}
}