标签:eof getname 老师 接口 静态成员 class 就会 setname 成员
class Demo1_Polymorphic {
public static void main(String[] args) {
Cat c = new Cat();
c.eat();
Animal a = new Cat(); //猫是一只动物 //父类引用指向子类对象
a.eat();
}
}
class Animal {
public void eat() {
System.out.println("动物吃饭");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("猫吃鱼");
}
}
运行结果为猫吃鱼 猫吃鱼
运行结果为 10 20
将Father.class 中的print方法注释掉就会报错:找不到符号 f.print();
运行结果为 son
class Demo2_Polymorphic {
public static void main(String[] args) {
Father f = new Son();
f.method(); //相当于是Father.method()
}
}
class Father {
public static void method() {
System.out.println("father static method");
}
}
class Son extends Father {
public static void method() {
System.out.println("son static method");
}
}
运行结果为father static method
class Demo3_SuperMan {
public static void main(String[] args) {
Person p = new SuperMan(); //父类引用指向子类对象,超人提升为了人,向上转型
//父类引用指向子类对象就是向上转型
System.out.println(p.name);
p.谈生意();
SuperMan sm = (SuperMan)p; //向下转型
sm.fly();
}
}
class Person {
String name = "John";
public void 谈生意() {
System.out.println("谈生意");
}
}
class SuperMan extends Person {
String name = "superMan";
public void 谈生意() {
System.out.println("谈几个亿的大单子");
}
public void fly() {
System.out.println("飞出去救人");
}
}
运行结果为 John 谈几个亿的大单子 飞出去救人
class Demo4_Animal {
public static void main(String[] args) {
//Cat c1 = new Cat();
//c1.eat();
method(new Cat());
method(new Dog());
//Animal a = new Cat(); 实际开发中很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中的特有属性和行为
}
//Cat c = new Dog();狗是一只猫,这是错误的
/*public static void method(Cat c) {
c.eat();
}
public static void method(Dog d) {
d.eat();
}*/
//如果把狗强转成猫就会出现类型转换异常,ClassCastException
public static void method(Animal a) { //当作参数的时候用多态最好,因为扩展性强
//关键字 instanceof 判断前边的引用是否是后边的数据类型
if (a instanceof Cat) {
Cat c = (Cat)a;
c.eat();
c.catchMouse();
}else if (a instanceof Dog) {
Dog d = (Dog)a;
d.eat();
d.lookHome();
}else {
a.eat();
}
}
}
class Animal {
public void eat() {
System.out.println("动物吃饭");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("猫吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("狗吃肉");
}
public void lookHome() {
System.out.println("看家");
}
}
class Demo1_Abstract {
public static void main(String[] args) {
//Animal a = new Animal(); //错误: Animal是抽象的; 无法实例化
Animal a = new Cat(); //父类引用指向子类对象
a.eat();
}
}
abstract class Animal { //抽象类
public abstract void eat(); //抽象方法
public Animal() {
System.out.println("父类空参构造");
}
}
class Cat extends Animal {
public Cat() {
super();
}
public void eat() {
System.out.println("猫吃鱼");
}
}
运行结果为 父类空参构造 猫吃鱼
class Test1_Animal {
public static void main(String[] args) {
Cat c = new Cat("加菲",8);
System.out.println(c.getName() + "..." + c.getAge());
c.eat();
c.catchMouse();
Dog d = new Dog("八公",30);
System.out.println(d.getName() + "..." + d.getAge());
d.eat();
d.lookHome();
}
}
abstract class Animal {
private String name; //姓名
private int age; //年龄
public Animal(){} //空参
public Animal(String name,int age) {//有参
this.name = name;
this.age = age;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public abstract void eat(); //吃饭
}
class Cat extends Animal {
public Cat(){} //空参
public Cat(String name,int age) {//有参
super(name,age);
}
public void eat() {
System.out.println("猫吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public Dog(){} //空参
public Dog(String name,int age) {//有参
super(name,age);
}
public void eat() {
System.out.println("狗吃肉");
}
public void lookHome() {
System.out.println("看家");
}
}
class Test2_Teacher {
public static void main(String[] args) {
BaseTeacher bt = new BaseTeacher("冯佳",18);
bt.teach();
}
}
abstract class Teacher {
private String name; //姓名
private int age; //年龄
public Teacher(){} //空参
public Teacher(String name,int age) {//有参
this.name = name;
this.age = age;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public abstract void teach();
}
class BaseTeacher extends Teacher {
public BaseTeacher(){} //空参
public BaseTeacher(String name,int age) {//有参
super(name,age);
}
public void teach() {
System.out.println("我的姓名是:" + this.getName() + ",我的年龄是:" + this.getAge() + ",讲的内容是java基础");
}
}
标签:eof getname 老师 接口 静态成员 class 就会 setname 成员
原文地址:https://www.cnblogs.com/albieh/p/12207407.html