面向对象

包:

package com.bit;
==================================
import java.util.Date; //引入java.util中的data类

包访问权限
常见的系统包

  1. java.lang:系统常用基础类(String、Object),此包从JDK1.1后自动导入。
  2. java.lang.reflect:java 反射编程包;
  3. java.net:进行网络编程开发包。
  4. java.sql:进行数据库开发的支持包。
  5. java.util:是java提供的工具程序包。(集合类等) 非常重要
  6. java.io:I/O编程开发包

继承:

public class Animal {
    protected String name;
    /*static {
        System.out.println("Animal::static{}");
    }
    {
        System.out.println("Animal::instance{}");
    }*/
    public Animal(String name) {
        this.name = name;
        //System.out.println("Animal(String)");
    }
    public void eat() {
        System.out.println(this.name+"eat()!");
    }
}
public class Bird extends Animal{
    private String yumao;
   /* static {
        System.out.println("Bird::static{}");
    }
    {
        System.out.println("Bird::instance{}");
    }*/
    public Bird(String name,String yumao) {
        super(name);
        this.yumao = yumao;
        //System.out.println("Bird(String,String)");
    }
    public void fly() {
        System.out.println(this.name+"fly");
    }
}
public class Cat extends Animal{
    private String color;//子类所独有的数据
    /*static {
        System.out.println("Cat::static{}");
    }
    {
        System.out.println("Cat::instance{}");
    }*/
    public Cat(String name,String color) {
        //必须是第一行-》子类构造的时候 需要先构造父类
        super(name);//显示的调用父类的构造方法-》帮助父类来进行构造
        this.color = color;
        //System.out.println("Cat(String,String)");
    }

    public void func1() {
        System.out.println("func1()");
       /*String name = super.name;
       super.eat();*/
    }
}

class ChineseGardenCat extends Cat{
    public String sex;
    public ChineseGardenCat(String name,String color,String sex) {
        super(name,color);
        this.sex = sex;
    }
}

final class A( ):密封类,一旦一个类被final所修饰就不能被继承

运行时绑定(动态绑定)
1.向上转型
2.父类和子类都有同名的覆盖方法(重写)

class Animal{
    public String name;
    public Animal(String name) {
        this.name = name;
        //eat();
    }
    public void eat() {
        System.out.println(this.name+"Animal::eat");
    }
}
class Cat extends Animal{
    public String sex;
    public Cat(String name,String sex) {
        super(name);
        this.sex = sex;
    }
    public void jump() {
        System.out.println(this.name+"jump");
    }

    @Override//重写(Ctrl+o )
    public void eat() {
        System.out.println(this.name+"Cat::eat");
    }
}

class Bird extends Animal{
    public Bird(String name) {
        super(name);
    }

    public void fly(){
        System.out.println(this.name+"fly");
    }
}

public class TestDemo1 {

    public static void func() {

    }

    public static void main(String[] args) {
        Animal animal = new Cat("小花","man");
        animal.eat();
       /* func();*/
    }

    /*public static void main1(String[] args) {
        Animal animal = new Cat("小花","man");
        animal.eat();

        //向下转型
        Animal animal2 = new Bird("小明");
        animal2.eat();
        Bird bird = (Bird) animal2;
        bird.fly();

        //注意--》
        Animal animal3 = new Cat("小明","man");
        if(animal3 instanceof Bird) {
            Bird bird2 = (Bird) animal3;
        }
    }*/
}
    小花Cat::eat(发生动态绑定)

super this
在这里插入图片描述
抽象类(abstract):

/**
 * 1、包含抽象方法的类  叫做抽象类
 * 2、抽象类和普通类 最大的区别 包含抽象方法
 * 3、抽象类不能被实例化   不能new
 * 4、抽象类存在的意义-》肯定要被继承
 * 5、抽象类一旦被继承 就要重写抽象方法
 * 6、如果一个类继承了抽象类 那么如果不重写抽象类的
 * 方法,那么当前类 需要设计为抽象类-》
 * 可以选择重写也可以不重写。
 * 7.被继承不能加final
 */
abstract class Shape2 {
    /* public int a;
     public void func() {
         System.out.println("func()");
     } */
    public abstract void draw();//抽象方法
}
 class Rect2 extends Shape2 {
    @Override
    public void draw() {
        System.out.println("♦");
    }
    //public abstract void func();
}
/*class Rect3 extends Rect2 {
    @Override
    public void draw() {
        System.out.println("♦");
    }
}*/
class Cycle2 extends Shape2{
    @Override
    public void draw() {
        System.out.println("○");
    }
}
class Flower2 extends Shape2{
    @Override
    public void draw() {
        System.out.println("❀");
    }
}
class Triangle2 extends Shape2 {
    @Override
    public void draw() {
        System.out.println("△");
    }
}
public class TestDemo3 {
    public static void drawMap (Shape2 shape2) {
        shape2.draw();
    }
    public static void main(String[] args) {
        //提前检查代码的错误
        //Shape2 shape2 = new Shape2();

        Rect2 rect2 = new Rect2();
        Cycle2 cycle2 = new Cycle2();
        Flower2 flower2 = new Flower2();
        drawMap(rect2);
        drawMap(cycle2);
        drawMap(flower2);
    }
}

接口(interface):
接口的出现解决多继承问题

/**
 * 1、定义的方法默认是:public abstract
 * 2、定义的成员变量默认是: public static final
 * 3、尽量简洁-》不用加上面所列内容
 * 4、类和接口的关系-》实现:**implements**
 * 5、实现了接口 必须重写方法
 */
interface IShape{
    //int a = 10;//成员变量
    void draw();
}
class Cycle implements IShape{
    @Override//重写
    public void draw() {
        System.out.println("○");
    }
}
class Flower implements IShape{
    @Override
    public void draw() {
        System.out.println("❀");
    }
}
class Triangle implements IShape {
    @Override
    public void draw() {
        System.out.println("△");
    }
}
public class TestDemo4 {
    public static void drawMap (IShape shape) {
        shape.draw();
    }
    public static void main(String[] args) {
        //IShape shape = new IShape();
        IShape shape = new Cycle();//向上转型
        Cycle cycle = new Cycle();
        drawMap(shape);
        drawMap(cycle);
    }
}
interface  A {
    void funcA();
}
interface B {
    void funcB();
}
interface D extends A,B{//extends扩展 接口与接口
    void funcD();
}
class F implements D{
    @Override
    public void funcA() {

    }
    @Override
    public void funcB() {

    }
    @Override
    public void funcD() {

    }
}

//.........
class C implements A,B {

    @Override
    public void funcA() {

    }

    @Override
    public void funcB() {

    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值