zoukankan      html  css  js  c++  java
  • 动手动脑

    1.

    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();
        }
    
    }
    运行结果:

    ,在parent中,注释部分必须父类在前,否则出错!

    2.

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

    3.
    1 public class ParentChildTest {
     2     public static void main(String[] args) {
     3         Parent parent=new Parent();
     4         parent.printValue();//1
     5         Child child=new Child();
     6         child.printValue();//2
     7         
     8         parent=child;//将子类赋值给父类value变为200
     9         parent.printValue();//3,输出200
    10         
    11         parent.myValue++;//父类的value+1,对子类的value之不改变
    12         parent.printValue();//4
    13         
    14         ((Child)parent).myValue++;//先进行强制转化变为子类类型,值不会变化,在加一201
    15         parent.printValue();
    16         
    17     }
    18 }
    19 
    20 class Parent{
    21     public int myValue=100;//101
    22     public void printValue() {
    23         System.out.println("Parent.printValue(),myValue="+myValue);
    24     }
    25 }
    26 class Child extends Parent{
    27     public int myValue=200;//201
    28     public void printValue() {
    29         System.out.println("Child.printValue(),myValue="+myValue);
    30     }
    31 }
    运行结果:

    因为子类继承了父类,所以子类可以赋值给父类,但是父类不可以赋值给子类,使用关键字super,可以调用父类的对象

  • 相关阅读:
    CentOS安装node.js-8.11.1+替换淘宝NPM镜像
    【推荐】CentOS安装gcc-4.9.4+更新环境+更新动态库
    申请安装阿里云免费SSL证书
    服务器安全加固
    【推荐】优秀代码
    CenOS登录失败处理功能
    Ubuntu修改密码及密码复杂度策略设置
    mysql 5.7添加server_audit 安全审计功能
    快速安装jumpserver开源堡垒机
    oracle 11g 配置口令复杂度
  • 原文地址:https://www.cnblogs.com/yishaui/p/9926298.html
Copyright © 2011-2022 走看看