Day15

package com.oop.demo06;

public class Person {
public void run(){
System.out.println("run");
}
}



package com.oop.demo06;

public class Student extends Person{
public void go(){
System.out.println("go");
}
}


/*
package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
public static void main(String[] args) {
//Object->String
//Object->person->Teacher
//Object->Person->Student
Object object=new Student();

//System.out.println(x instanceof y);能不能编译通过!就是看x和y之间是否存在父子关系


System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("==================");
Person person=new Person();
System.out.println(person instanceof Student);//flase
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译报错
System.out.println("==================");
Student student=new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);//编译报错
//System.out.println(student instanceof String);//编译报错
}
}
*/









/*
多态注意事项:
1、多态是方法的多态,属性没有多态
2、父类和子类有联系 类型转换异常(ClassCastException)
3、存在条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1=new son();


不能重写,也没有多态的方法
1、static 方法,属于类,它不属于示例
2、final 常量
3、private方法
*/




package com.oop.demo06;

public class Teacher extends Person{
}



package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;

public class Application {
public static void main(String[] args) {
//类型直接的转换:基本类型转换 父 子
//高 低
Person s1=new Student();
//student这个对象转换成Student类型,我们就可以使用Student类型的方法
Student student= (Student)s1;//高转低强制转换
//子类转换成父类可能会丢失一些方法
Student s2=new Student();
s2.go();
Person person=student;
}
}
/*
1、父类引用指向子类对象
2、把子类转换成父类,向上转型
3、把父类转换成子类,向下转型,需强制转换
4、方便方法的调用,减少重复的代码
*/

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Day15