百木园-与人分享,
就是让自己快乐。

Day14 note1

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 run(){
System.out.println(\"son\");
}
public void eat(){
System.out.println(\"eat\");
}
}


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


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



package com.oop;

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

public class Application {
public static void main(String[] args) {
/*
Student s1=new Student();
s1.setName(\"lisa\");
System.out.println(s1.getName());
s1.setAge(150);//不合法的数据
System.out.println(s1.getAge());

*/
//demo06测试
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了
//Student能够调用的方法都是自己的方法或者继承父类的
Student s1=new Student();
//Person可以指向子类,但是不能调用子类独有的方法
Person s2=new Student();//父类的引用指向子类
Object s3=new Student();
//s2.eat();不能运行,因为对象能执行的哪些方法,主要看对象的左边的类型,和右边关系不大
((Student)s2).eat();//强制转换
s2.run();
s1.run();
s1.eat();
}
}




来源:https://www.cnblogs.com/tomn/p/16241270.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Day14 note1

相关推荐

  • 暂无文章