zoukankan      html  css  js  c++  java
  • 第六周动手动脑

    关于继承中的子类与父类的构造方法的使用

    class Grandparent 
    {
    
    
        public Grandparent()
         {
    
                System.out.println("GrandParent Created.");
        
    }
    
    
        public Grandparent(String string) 
        {
    
                System.out.println("GrandParent Created.String:" + string);
        
     }
    
    }
    
    
    
    class Parent extends Grandparent
    {
    
    
        public Parent()
         {
    
                super("Hello.Grandparent.");
    
                System.out.println("Parent Created");
        
            //super("Hello.Grandparent.");
    
          }
    
    }
    
    
    
    class Child extends Parent 
    {
    
    
        public Child()
         {
        
            System.out.println("Child Created");
    
          }
    
    }
    
    
    
    public class TestInherits 
    {
    
    
        public static void main(String args[])
         {
    
                Child c = new Child();
        
      }
    
    }

    运行结果:

    GrandParent Created.
    Parent Created
    Child Created

    从运行结果可以看出构造函数先运行父类再运行子类,并且通过修改上述代码可以看出super()只能在构造函数的第一行。

    构造方法主要的作用就是初始化,初始化时需要先将父类的内容初始化再初始化自己的内容

    final class 类名()是不可继承类,也就是所谓的“断子绝孙”类。

    package one;
    public class ExplorationJDKSource {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            System.out.println(new A());
        }
    
    }
    
    class A{}

    对该代码进行反汇编

     进入println的源代码:

     进入valueOf源代码:

     输出类 是输出的包名.类名@地址

    package one;
    public class Fruit
    {
            
        public String toString()
        {
            return "Fruit toString.";
        }
    
        public static void main(String args[])
        {
            Fruit f=new Fruit();
            System.out.println("f="+f);
        //    System.out.println("f="+f.toString());
        }
    }

    这段代码是该类覆盖了object父类的toString类

    package one;
    
    public class text {
        public void show()
        {
            System.out.println("父类方法");
        }
    
    }
    package one;
    
    public class text1 extends text {
        public text1()
        {
            show();
        }
        public void show()
        {
            //super.show();
            System.out.println("这是子类");
        }
        public static void main(String[] args) {
            new text1();
        }
    
    }

    上面两个代码是自己码的一个覆盖类,如果想要调用父类的被覆盖的类值能使用super.方法();的方法调用父类方法

    package one;
    class Mammal{}
    class Dog extends Mammal {}
    class Cat extends Mammal{}
    
    public class TestCast
    {
        public static void main(String args[])
        {
            Mammal m;
            Dog d=new Dog();
            Cat c=new Cat();
            m=d;
            //d=m;
            //d=(Dog)m;
            //d=c;
            //c=(Cat)m;
    
        }
    }

    该代码运行时,

    m=d;不会出错;

    d=m;会出错;

    d=(Dog)m;也会出错;

    d=c;也会出错;

    c=(Cat)m;也会出错;

    总结上述内容,子类可以给父类赋值,父类给子类赋值会出错;

    package one;
    public class ParentChildTest {
        public static void main(String[] args) {
            Parent parent=new Parent();
            parent.printValue();
            Child child=new Child();
            child.printValue();
            
            parent=child;
            parent.printValue();
            
            parent.myValue++;
            parent.printValue();
            
            ((Child)parent).myValue++;
            parent.printValue();
            
        }
    }
    
    class Parent{
        public int myValue=100;
        public void printValue() {
            System.out.println("Parent.printValue(),myValue="+myValue);
        }
    }
    class Child extends Parent{
        public int myValue=200;
        public void printValue() {
            System.out.println("Child.printValue(),myValue="+myValue);
        }
    }

    在子类与父类方法变量等相等时,父类对象调用父类方法 子类对象调用子类方法

    子类对象在调用相同方法时 如果与父类方法同名则用“局部优先”原则,运行子类方法;

    多态的好处就是可以在父类定义一个飞行的方法,然后子类可以根据自己的需要定义不同飞的方法和在何处飞等。

    package zoo3;
    
    
    public class Zoo {
    
        public static void main(String args[]) {
            Feeder f = new Feeder("小李");
            Animal[] ans = new Animal[16];
    
            //饲养员小李喂养一只狮子
            ans[0] = new Lion();
            //饲养员小李喂养十只猴子
            for (int i = 0; i < 10; i++) {
                ans[1 + i] = new Monkey();
            }
            //饲养员小李喂养5只鸽子
            for (int i = 0; i < 5; i++) {
                ans[11 + i] = new Pigeon();
            }
    
            f.feedAnimals(ans);
        }
    }
    
    class Feeder {
    
        public String name;
    
        Feeder(String name) {
            this.name = name;
        }
    
        public void feedAnimals(Animal[] ans) {
            for (Animal an : ans) {
                an.eat();
            }
        }
    }
    
    abstract class Animal {
    
        public abstract void eat();
    }
    
    class Lion extends Animal {
    
        public void eat() {
            System.out.println("我不吃肉谁敢吃肉!");
        }
    }
    
    class Monkey extends Animal {
    
        public void eat() {
            System.out.println("我什么都吃,尤其喜欢香蕉。");
        }
    }
    
    class Pigeon extends Animal {
    
        public void eat() {
            System.out.println("我要减肥,所以每天只吃一点大米。");
        }
    }

    这种代码的优化可以使用一个而实例化所有类的对象

  • 相关阅读:
    PHP中功能强大却少使用的函数
    php 判断美国zip code
    php 操作 MySQL 中的Blob类型
    THINKPHP 多域名 MEMCACHE方式共享SESSION数据
    phpQuery—基于jQuery的PHP实现
    Error calling method on NPObject!
    国外共享软件热门上载站点
    你留意过自己的父母吗?
    域名LOGO产生器 (全智能)
    想要创业者须知 企业注册商标6大步
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/11742132.html
Copyright © 2011-2022 走看看