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("猫吃鱼");
}
}
class Demo2_Polymorphic {
public static void main(String[] args) {
Father f = new Son(); //父类引用指向子类对象
System.out.println(f.num); //相当于是Father.method()
Son s = new Son();
System.out.println(s.num);
}
}
class Father {
int num = 10;
}
class Son extends Father {
int num = 20;
class Demo2_Polymorphic {
public static void main(String[] args) {
Father f = new Son();
f.print(); //相当于是Father.method()
}
}
class Father {
int num = 10;
public void print() {
System.out.println("father");
}
}
class Son extends Father {
int num = 20;
public void print() {
System.out.println("son");
}
}
class Demo2_Polymorphic {
public static void main(String[] args) {
Father f = new Son();
f.method(); //相当于是Father.method()
}
}
class Father {
int num = 10;
public static void method() {
System.out.println("father static method");
}
}
class Son extends Father {
int num = 20;
public static void method() {
System.out.println("son static method");
}
}
class Demo3_SuperMan {
public static void main(String[] args) {
Person p = new SuperMan(); //父类引用指向子类对象,超人提升为了人
//父类引用指向子类对象就是向上转型
System.out.println(p.name);
p.talkTrade();
}
}
class Person {
String name = "John";
public void talkTrade() {
System.out.println("谈生意");
}
}
class SuperMan extends Person {
String name = "superMan";
public void talkTrade() {
System.out.println("谈几个亿的大单子");
}
public void fly() {
System.out.println("飞出去救人");
}
class Demo3_SuperMan {
public static void main(String[] args) {
Person p = new SuperMan(); //父类引用指向子类对象,超人提升为了人
//父类引用指向子类对象就是向上转型
System.out.println(p.name);
p.talkTrade();
SuperMan sm = (SuperMan)p; //向下转型
sm.fly();
/*
基本数据类型自动类型提升和强制类型转换
*/
int i = 10;
byte b = 20;
//i = b; //自动类型提升
//b = (byte)i; //强制类型转换
}
}
class Person {
String name = "John";
public void talkTrade() {
System.out.println("谈生意");
}
}
lass SuperMan extends Person {
String name = "superMan";
public void talkTrade() {
System.out.println("谈几个亿的大单子");
}
public void fly() {
System.out.println("飞出去救人");
}
}
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 Fu {
public void show() {
System.out.println("fu show");
}
}
class Zi extends Fu {
public void show() {
System.out.println("zi show");
}
public void method() {
System.out.println("zi method");
}
}
class Test1Demo {
public static void main(String[] args) {
Fu f = new Zi();
f.method();
f.show();
}
}错误 多态中父类无法子类的特有方法
class A {
public void show() {
show2();
}
public void show2() {
System.out.println("我");
}
}
class B extends A {
public void show2() {
System.out.println("爱");
}
}
class C extends B {
public void show() {
super.show();
}
public void show2() {
System.out.println("你");
}
}
public class Test2DuoTai {
public static void main(String[] args) {
A a = new B();
a.show();//爱
B b = new C();
b.show();//你
}
}
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 Demo2_Abstract {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
abstract class Demo {
int num1 = 10;//变量
final int num2 = 20;//常量
public Demo(){}//构造方法
public void print() {//非抽象方法,子类可以直接使用
System.out.println("111");
}
public abstract void method();//抽象方法,子类必须实现
}
class Test extends Demo {
public void method() {
System.out.println("111");
}
}
class Demo3_葵花宝典 {
public static void main(String[] args) {
岳不群 小岳子 = new 岳不群();
小岳子.自宫();
}
}
abstract class 葵花宝典 {
public abstract void 自宫();
}
class 岳不群 extends 葵花宝典 {
public void 自宫() {
System.out.println("用牙签");
}
}
class 林平之 extends 葵花宝典 {
public void 自宫() {
System.out.println("用指甲刀");
}
}
class 东方不败 extends 葵花宝典 {
public void 自宫() {
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基础");
}
}
class Test3_Employee {
public static void main(String[] args) {
Coder c = new Coder("德玛西亚","007",8000);
c.work();
Manager m = new Manager("苍老师","9527",3000,20000);
m.work();
}
}
abstract class Employee {
private String name; //姓名
private String id; //工号
private double salary; //工资
public Employee() {} //空参构造
public Employee(String name,String id,double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setId(String id) { //设置id
this.id = id;
}
public String getId() { //获取id
return id;
}
public void setSalary(double salary) { //设置工资
this.salary = salary;
}
public double getSalary() { //获取工资
return salary;
}
public abstract void work();
}
//程序员
class Coder extends Employee {
public Coder() {} //空参构造
public Coder(String name,String id,double salary) {
super(name,id,salary);
}
public void work() {
System.out.println("我的姓名是:" + this.getName() + ",我的工号是:" + this.getId() + ",我的工资是:"
+ this.getSalary() + ",我的工作内容是敲代码");
}
}
//项目经理
class Manager extends Employee {
private int bonus; //奖金
public Manager() {} //空参构造
public Manager(String name,String id,double salary,int bonus) {
super(name,id,salary);
this.bonus = bonus;
}
public void work() {
System.out.println("我的姓名是:" + this.getName() + ",我的工号是:" + this.getId() + ",我的工资是:"
+ this.getSalary() + ",我的奖金是:" + bonus + ",我的工作内容是管理");
}
}
class Demo4_Abstract {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
/*
* A:面试题1
* 一个抽象类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?
* 可以
* 这么做目的只有一个,就是不让其他类创建本类对象,交给子类完成
* B:面试题2
* abstract不能和哪些关键字共存
abstract和static
被abstract修饰的方法没有方法体
被static修饰的可以用类名.调用,但是类名.调用抽象方法是没有意义的
abstract和final
被abstract修饰的方法强制子类重写
被final修饰的不让子类重写,所以他俩是矛盾
abstract和private
被abstract修饰的是为了让子类看到并强制重写
被private修饰不让子类访问,所以他俩是矛盾的
*/
abstract class Demo {
//public static abstract void print(); //错误: 非法的修饰符组合: abstract和static
//public final abstract void print(); //错误: 非法的修饰符组合: abstract和final
private abstract void print(); //错误: 非法的修饰符组合: abstract和private
}
class Demo1_Interface {
public static void main(String[] args) {
//Inter i = new Inter(); //接口不能被实例化,因为调用抽象方法没有意义
Inter i = new Demo(); //父类引用指向子类对象
i.print();
}
}
interface Inter {
public abstract void print(); //接口中的方法都是抽象的
}
class Demo implements Inter {
public void print() {
System.out.println("print");
}
}
class Demo2_Interface {
public static void main(String[] args) {
Demo d = new Demo();
d.print();
System.out.println(Inter.num);
}
}
interface Inter {
public static final int num = 10;
//public Inter(){} 接口中没有构造方法
/*public void print() { 接口中不能定义非抽象方法
}*/
public abstract void print();
}
class Demo /extends Object/ implements Inter { //一个类不写继承任何类,默认继承Object类
public void print() {
//num = 20;
System.out.println(num);
}
public Demo() {
super();
}
}
class Demo3_Interface {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
interface InterA {
public abstract void printA();
}
interface InterB {
public abstract void printB();
}
interface InterC extends InterB,InterA {
}
//class Demo implements InterA,implements InterB { //这么做不允许是非法的
class Demo extends Object implements InterA,InterB {
public void printA() {
System.out.println("printA");
}
public void printB() {
System.out.println("printB");
}
}
A:成员区别
B:关系区别
C:设计理念区别
class Test1_Animal {
public static void main(String[] args) {
Cat c = new Cat("加菲",8);
c.eat();
c.sleep();
JumpCat jc = new JumpCat("跳高猫",3);
jc.eat();
jc.sleep();
jc.jump();
}
}
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(); //吃饭
public abstract void sleep(); //睡觉
}
interface Jumping { //跳高的接口
public void jump();
}
class Cat extends Animal {
public Cat() {} //空参构造
public Cat(String name,int age) {//有参构造
super(name,age);
}
public void eat() {
System.out.println("猫吃鱼");
}
public void sleep() {
System.out.println("侧着睡");
}
}
class JumpCat extends Cat implements Jumping {
public JumpCat() {} //空参构造
public JumpCat(String name,int age) {//有参构造
super(name,age);
}
public void jump() {
System.out.println("猫跳高");
}
}
总结起来好累啊!