java继承
发布日期:2022-02-08 18:03:14 浏览次数:23 分类:技术文章

本文共 767 字,大约阅读时间需要 2 分钟。

  1. 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大!

//继承实例

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(" My Childen");//重载的调用,必须是子类构造方法的第一句****

                     System.out.println("Parent Created");

              }

       }

      

      

       class Child extends Parent{

              public Child(){

                    

                     System.out.println("Child Created");

              }

       }

      

//     输出顺序()

      

public class jicheng {

      

       public static void main(String[] args) {

              // TODO 自动生成的方法存根

              Child a=new Child();

              /*Parent b=new Parent();*/

 

       }

 

}

 

转载于:https://www.cnblogs.com/ZHAOHAN666/p/6052261.html

转载地址:https://blog.csdn.net/dia92159/article/details/101278794 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:软件工程个人作业01
下一篇:Java的string

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月17日 13时35分45秒