【包,继承,多态,接口,抽象类,重写】

5

目录

静态导入

继承

动态绑定

方法重写

多态

一个坑

抽象类

抽象类的意义

接口

接口间的继承

clonable接口

深拷贝VS浅拷贝


包是组织类的一种方式,包的主要目的是保证类的唯一性。

 public static void main(String[] args) {
        java.util.Date date=new java.util.Date();
        System.out.println(date.getTime());
    }

import java.util.Date;

public class WanYiHa {
    public static void main(String[] args) {
        Date date =new Date();
        System.out.println(date.getTime());
    }
}

 

 

静态导入

 

继承

package Animal;

public class Test {
    public static void main(String[] args) {
        Cat cat=new Cat("小猫");
        cat.eat("吃的 ");
        Bird bird=new Bird("小鸟");
        bird.eat("吃的");
    }
}
class Animal {
    public String name;

    public Animal(String name) {
        this.name = name;
    }
    public void eat(String food){
        System.out.println(this.name+"正在吃"+food);
    }
}
// extends : 拓展的意思。
class Cat extends  Animal{
    public Cat(String name) {
        //  super 调用父类的构造方法
        super(name);
    }
}
class Bird extends Animal{

    public Bird(String name) {
        super(name);
    }
    public void fly(){
        System.out.println(this.name+"正在飞");
    }
}

class被 public修饰的化,不同位置的包之间都可以extends; 

protected 不能修饰class ;

 protected修饰父类一个非本质属性之后,A包的子包B下的类继承了A包目录下的父类,也可以访问这个被protected修饰的非本质属性。

向上转型 :

 在主方法中用

 

public class Test {
    public static void main(String[] args) {
//        Cat cat=new Cat("小猫");
//        cat.eat("吃的 ");
//        bird.eat("吃的");

        Animal bird3=new Bird("大鸟");
        feed(bird3);
    }
    public static void feed(Animal animal){
        animal.eat("谷子");
    }
}
class Animal {
    public String name;

    public Animal(String name) {
        this.name = name;
    }
    public void eat(String food){
        System.out.println(this.name+"正在吃"+food);
    }
}

动态绑定

 就是看对象选方法

package Animal;

public class Animal {
    // protected 修饰,子类可以使用 这条String 属性。
    protected  String name;
    // Animal 的构造方法 ,所以在new的时候初始化,给Animal 的对象都要求附一个名字; 比如"豆豆,鹦鹉"
    public Animal (String name){
        this.name=name;
    }
    // 父类有一个eat方法,和子类的不干预。 在使用 Animal 的对象时,调用这个eat() 方法
    public  void eat(String food){
        System.out.println(th
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值