zoukankan      html  css  js  c++  java
  • java基础(七)之子类实例化

    知识点;
    1、生成子类的过程
    2、使用super调用父类构造函数的方法

    首先编写3个文件。

    Person.java

    class Person{
    	String name;
    	int age;
    	Person(){
    		System.out.println("None");
    	}
    	Person(String name,int age){
    		this.name = name;
    		this.age = age;
    		
    		System.out.println("Name: " + this.name + " Age:" +this.age);
    	}
    	
    	void eat(){
    		System.out.println("eating");
    	}
    }
    

      

    Student.java:

    class Student extends Person{
    	Student(){
    		System.out.println("i am students!");
    	}
    }
    

      

    Test.java:

    public class Test{
    	public static void main(String[] args){
    		Student li = new Student();
    	}
    }
    

      

    编译运行结果是:

    PS C:UsersadminDesktopjava_code练习六> javac .Test.java
    PS C:UsersadminDesktopjava_code练习六> java Test
    None
    i am students!
    

      

    为什么运行的结果是这样呢?

    在子类的构造函数当中,必须调用父类的构造函数。
    如果没有的话,编译器会自动加上super,super会调用父类的构造函数:

    super();
    

    那为什么要"在子类的构造函数当中,必须调用父类的构造函数"?

    可以减少重复代码。

    请看下面的例子:

    Person.java:

    class Person{
    	String name;
    	int age;
    	Person(){
    		System.out.println("None");
    	}
    	Person(String name,int age){
    		this.name = name;
    		this.age = age;
    		
    	}
    	
    	void eat(){
    		System.out.println("eating");
    	}
    }
    

      

    如果按照以前的方式写,代码会是这样的,可以发现Stduent和Person类中有重复的代码。

    Student.java:

    class Student extends Person{
    	int grade;
    	Student(){
    		System.out.println("i am students!");
    	}
    	Student(String name,int age,int grade){
    		this.name = name;
    		this.age = age;
    		this.grade = grade;
    		
    	}
    }
    

      

    修改后Student.java:

    class Student extends Person{
    	int grade;
    	Student(){
    		System.out.println("i am students!");
    	}
    	Student(String name,int age,int grade){
    		super(name,age);
    		this.grade = grade;
    		
    	}
    }
    

      

    Test.java:

    public class Test{
    	public static void main(String[] args){
    		Student li = new Student("liming",18,90);
    		System.out.println(li.name);
    		System.out.println(li.age);
    		System.out.println(li.grade);
    	}
    }
    

      

    编译后输出:

    PS C:UsersadminDesktopjava_code练习六> java Test
    liming
    18
    90
    

      

    因此super与this调用构造函数类似,this()为空的时候调用同类无参数的构造函数,因此super会调用父类无参数的构造函数。

    • this()调用本类当中的构造函数
    • this. 调用本类中的成员变量
    • super() 调用父类当中的构造函数
    • super. 调用父类中的成员变量
  • 相关阅读:
    BZOJ 1597 [Usaco2008 Mar]土地购买 (斜率优化dp)
    HDU 6602 Longest Subarray (线段树)
    HDU 6521 K-th Closest Distance (主席树+二分)
    2019牛客多校2 H Second Large Rectangle(悬线法)
    The 2019 University of Jordan Collegiate Programming Contest
    CLR via C# 阅读 笔记
    C# 访问https 未能创建 SSL/TLS 安全通道
    转载文章——Datatable删除行的Delete和Remove方法
    ASP.NET Request.UrlReferrer 问题
    ASP.NET WebMethod方法使用 、AngularJS $http请求、 Jquery $.ajax请求
  • 原文地址:https://www.cnblogs.com/endust/p/11813368.html
Copyright © 2011-2022 走看看