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("猫吃鱼");
}
}
编译的时候看父类中有没有print方法,如果有,运行的时候执行的是子类中的print方法,如果没有就会直接报错
/*
基本数据类型自动类型提升和强制类型转换
*/
int i = 10;
byte b = 20;
//i = b; //自动类型提升(小的提升为大的,byte类型2个字节,int类型分配4个字节)
//b = (byte)i; //强制类型转换(精度降低)
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();
//p.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("飞出去救人");
}
}
向下转型以后,p的地址直接复制给了sm
class Demo4_Animal {
public static void main(String[] args) {
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();//zi 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();//编译看左边有show方法则编译通过,执行看右边,执行子类中的show方法,
//此show方法时继承父类的,父类中的show方法中有个show2方法调用的是子类的show2,故输出“爱”
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("猫吃鱼");
}
}
public class Demo2_Dollection {
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 this.name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.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 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 + ",我的工作内容是管理");
}
}
不能与static共存,被abstract修饰的方法没有方法体,被static修饰的可以用类名.调用,但是类名.调用抽象方法是没有意义的
不能与final共存,被abstract修饰的方法强制让子类重写,final修饰的不让子类重写,矛盾
不能与private共存,被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");
}
}
一个类不写继承任何类,默认继承Objedt类
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("猫跳高");
}
}