zoukankan      html  css  js  c++  java
  • Java(14):继承

    package zzz;
    
    // 创建继承
    
    public class Demo2 {
        
        public static void main(String[] args) {
            
            Animal animal = new Animal();
            animal.live();
            animal.run();
            
            System.out.println("******************************************");
            
            Cat catAnimal = new Cat();
            catAnimal.live();
            catAnimal.run();
            
            System.out.println("******************************************");
            
            SmallCat smallCat = new SmallCat();
            smallCat.run();
            smallCat.live();
            
            System.out.println("******************************************");
            
            Dog dog = new Dog();
            dog.run();
    
        }
        
    }
    
    
    class Animal {
        
        String animal = "animal";
        
        public Animal() {
            System.out.println("父类构造函数Animal结束.");
        }
        public void live() {
            System.out.println(animal+" live ...");
        }
        public void run() {
            System.out.println(animal + " can run ...");
        }
    }
    
    class Dog extends Animal {
        String animal = "dog";
        
        public Dog() {
            System.out.println("Dog() end.");
        }
        @Override
        public void run() {
            System.out.println(super.animal + " can run, " + this.animal + " can run ...");
        }
    }
    
    class Cat extends Animal {
    
        String animal = "cat";
        
        public Cat() {
    //        调用父类的构造函数
            super();
            System.out.println("子类构造函数Cat结束.");
        }
    
        @Override
        public void run() {
            System.out.println(super.animal + " can run, such as " + this.animal);
        }
        @Override
        public void live() {
    //        子类调用父类的方法
            super.live();
        }
    }
    
    class SmallCat extends Cat {
    
        String animal = "small cat";
        
        public SmallCat() {
    //        继承子类默认会调用父类的无参构造函数,隐式存在一个super();调用父类Cat类的构造函数
            System.out.println("子类构造函数SmallCat结束.");
        }
        @Override
        public void run() {
            System.out.println(this.animal + " can run .");
        }
        @Override
        public void live() {
            super.live();
        }
    }
  • 相关阅读:
    ESXi创建磁盘命令
    TNS-12518,TNS-12536,TNS-00506,Linux Error: 11: Resource temporarily unavailable
    监听的instance status blocked分析
    Oracle 用户、对象权限、系统权限
    MIME详解
    11g等待事件之library cache: mutex X
    Latch Free
    PowerDesigner小技巧
    yum本地源配置
    内核参数SEMMSL SEMMNS SEMOPM SEMMNI参数的设置
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/14014054.html
Copyright © 2011-2022 走看看