Day10

方法重写

public class B {
   public  void test(){
       System.out.println("B=>test()");
  }
}
//重写都是方法的重写,与属性无关
public class A extends B{
   //重写
   @Override//注解:有功能的解释
   public void test() {
       System.out.println("A=>test");
  }
}
//静态方法和非静态方法区别很大
public class ApplicationDemo07 {
   public static void main(String[] args) {
       //方法的调用只和左边定义的数据有关
       A a=new A();
       a.test();//A
    //父亲的引用指向子类
       B b = new A();//子类重写了父类的方法
       b.test();//A
//如果不重写,使用static则输出B
  }

}

多态

public class Person {
   public void run(){
       System.out.println("run");
  }
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父亲和子类,有联系   类型转换异常! ClassCastExcepttion!
3.存在条件:继承关系, 方法需要重写,父类引用指向子类对象!Father f1=new Son();
一下不能重写
   1.Static 方法,属于类,它不属于实例
   2.final 常量
   3.private方法
*/
public class Student extends Person {
   @Override
   public void run() {
       System.out.println("son");
  }
   public void eat(){
       System.out.println("eat");
  }
}
import oop.Demo08.Person;
import oop.Demo08.Student;

public class ApplicationDemo08 {
   public static void main(String[] args) {
     //一个对象的实际类型是确定的
       //new Student();
       //new Person();
       //可以指向的引用类型就不确定了,父亲的引用指向子类
       //Student能调用的方法都是自己的或者继承父类的
       Student s1 = new Student();
       Person s2= new Student();
       Object s3=new Student();
       s2.run();//子类重写了父类的方法,执行子类的方法
       s1.run();
       //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
      /* s2.eat();//报错,子类重写了父类的方法,执行子类的方法
       */
       s1 .eat();
      ((Student) s2).eat();
  }
}

instanceof(类型转换) 引用类型,判断一个对象是什么类型

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

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

/*
import oop.Demo09.Person1;
import oop.Demo09.Student;
import oop.Demo09.Teacher;
import org.omg.CORBA.Object;
*/
/**
* @author senko
* @date 2022/9/27 18:06
*/
public class ApplicationDemo09 {
   public static void main(String[] args) {
       //Object>String
       //Object>Person>Teavher
       //Object>Person>Student
       Object object = new Student();
       //System.out.println(X instanceof S);   编译能不能通过,看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 person1=new Student();
       System.out.println(person1 instanceof  Student);//true
       System.out.println(person1 instanceof Person);//true
       System.out.println(person1 instanceof Object);//true
       System.out.println(person1 instanceof Teacher);//false
      // System.out.println(person1 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);//编译错误
       System.out.println("======================================");
      //类型之间的转化: 父   子
       //高----》低   高转低可能会导致父类的一些方法流失
       Person obj=new Student();
       //Student1将这个对象转换为Student类型,我们就可以使用Student类型的方法了
       Student student1 = (Student) obj;
       student1.go();//这两行可以直接写成((Student)obj).go();
//低----》高

       Student student2 = new Student();
       student2.go();
       Person person=student;
  }
}
/*
1.父类引用指向子类对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型,强制转换
4.方便方法调用,减少重复的代码!简洁

抽象:封装,继承,多态
*/
 
posted @ 2022-09-27 19:55  宙斯xcl  阅读(155)  评论(0)    收藏  举报