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();
        }
    }
  • 相关阅读:
    方法名的string类型应用(补)
    unity3D里面的点乘和叉乘
    C# 计算时间日期
    iOS设备屏幕分辨率分布
    免证书发布ipa文件真机测试
    unity3D +php +数据库
    windows下mysql5.1忘记root密码解决方法[win7]
    springboot配置多数据源(JdbcTemplate方式)
    【转】Google Chrome中顺时针/逆时针滚动圆的含义
    Redis内存模型(2):存储细节
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/14014054.html
Copyright © 2011-2022 走看看