首页 > 其他分享 >Day10

Day10

时间:2022-09-27 20:14:36浏览次数:33  
标签:instanceof System Day10 Student println public out

方法重写

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.方便方法调用,减少重复的代码!简洁

抽象:封装,继承,多态
*/
 

标签:instanceof,System,Day10,Student,println,public,out
From: https://www.cnblogs.com/xclxcl/p/16735767.html

相关文章

  • 消息队列 day10
    RabbitMQ即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用。消息中间件在互联网公司的使用中越来越多,刚才还看到新闻阿里将Rocket......
  • 前端Day10
    视口(viewport):浏览器显示页面内容的屏幕区域。分为布局视口、视觉视口、理想视口。布局视口: 视觉视口: 理想视口: meta视口标签:width=device-width:布局视口宽......
  • Day10-CSS
    图片整合,精灵图,雪碧图:什么是图片整合: 1.把小的图片整合到一个大的图片上为什么要图片整合: 优点: 较少对服务器的请求次数 减少图片的内存 增加页面的加载速度 ......