• 学习java第29天


    一.java中的继承

    1.浅层克隆和深层克隆

    浅层克隆:主要复制基本对象值

    深层克隆:当类存在聚合关系时,必须考虑聚合对象的克隆

    2.java的继承

    使用关键字extends表示   class 子类 extends 父类{}

    public class Animal {

        private String name;  

        private int id;

        public Animal(String myName, String myid) {

            //初始化属性值

        }

        public void eat() {  //吃东西方法的具体实现  }

        public void sleep() { //睡觉方法的具体实现  }

    }

    public class Penguin  extends  Animal{

    }

    3.super关键字

    实现对父类的访问,用来引用当前对象的父类

    class Animal {

      void eat() {

        System.out.println("animal : eat");

      }

    class Dog extends Animal {

      void eat() {

        System.out.println("dog : eat");

      }

      void eatTest() {

        this.eat();                    // this 调用自己的方法

        super.eat();                 // super 调用父类方法

      }

    }

    public class Test {

      public static void main(String[] args) {

        Animal a = new Animal();

        a.eat();

        Dog d = new Dog();

        d.eatTest();

      }

    }

    4.类型检测——向上转型/向下转型

    向上转型语法:父类 对象 = new 子类 ()

     class Animal

     {

        public void eat()

        {

           System.out.println("父类的 eating...");

        }

    }

    class Bird extends Animal

    {   

        @Override 

        public void eat()

        {

           System.out.println("子类重写的父类的  eatting...");

        }  

        public void fly()

        {

           System.out.println("子类新方法  flying...");

        }

    }

    public class Sys

    {

        public static void main(String[] args)

        {

           Animal b=new Bird(); //向上转型

           b.eat();   

           sleep(new Male())

           sleep(new Female());

        }

        public static void sleep(Human h) 

            {

            h.sleep();

            }

    }

    向下转型格式:父类 对象1 = new 子类();

                             子类 对象2 = (子类) 对象 1;

     class Fruit

      {

        public void myName()

        {

           System.out.println("我是父类  水果...");

        }

    }

    class Apple extends Fruit

    {

        @Override

        public void myName()

        {

           System.out.println("我是子类  苹果...");

        }

        public void myMore()

        {

           System.out.println("我的苹果树");

        }

    }

    public class Sys{

        public static void main(String[] args) {

           Fruit a=new Apple(); 

           a.myName();

           Apple aa=(Apple)a;

           aa.myName();

           aa.myMore();

           Fruit f=new Fruit();

            Apple aaa=(Apple)f; 

           aaa.myName();

           aaa.myMore();

        }

    5.明天学习内容:动态绑定,静态变量,静态方法,静态代码块

  • 相关阅读:
    video和audio
    表单
    二叉树的先序遍历,后序遍历究竟是如何与快速排序,归并排序扯上关系?
    Python3下约瑟夫环的不同实现方式
    Golang下基于uint32的BitMap类库
    annotation
    流媒体技术
    C++系列之new 和 delete 的使用
    简易科学计算器的设计
    学生成绩档案管理系统的设计
  • 原文地址:https://www.cnblogs.com/SirNie/p/13426947.html
Copyright © 2020-2023  润新知