• super和this 在构造方法上的用法


    super

    Super()表示调用父类的构造方法。如果没有定义构造方法,那么就会调用父类的无参构造方法,即super()。

    this

    在构造方法中,this表示本类的其他构造方法:
    student(string n){

                               this();      //this表示调用这个类的student()
                             }
    如果调用student(int a)则为this(int a)。
    特别注意:用this调用其他构造方法时,this必须为第一条语句,然后才是其他语句。

     1 class Person{
     2 
     3 Person(){
     4 prt("A Person.");
     5 }
     6 
     7 Person(String name){
     8 prt("A person name is:"+name);
     9 }
    10 }
    11 
    12 public class Chinese extends Person{
    13    
    14 class Chinese{  
    15 
    16 Chinese(){
    17        super();   //调用父类构造函数
    18        prt("A chinese.");
    19               }
    20 
    21 Chinese(String name){
    22                super(name);    // 调用父类具有相同形参的构造函数,Person(String name)里的prt("A person name is:"+name)方法被调用。
    23                prt("his name is:"+name);
    24                        }
    25 
    26 Chinese(String name,int age){
    27                        this(name);//调用当前具有相同形参的构造函数,Chinese(String name)里prt("his name is:"+name);super(name);被调用。
    28                        prt("his age is:"+age);
    29                             }
    30 
    31 
    32 }
  • 相关阅读:
    From使用post与使用get区别
    HTML 自动跳转代码
    mjpgstreamer译文
    DOCUMENT.GETELEMENTBYID使用
    查看Linux内核版本的命令
    CGI编程学习5 穿插HTML,CSS零星知识
    使用Javascript显示时间
    北京大学<Perl 循序渐进>
    html之marquee详解
    Apache支持ASP.NET方法浅析
  • 原文地址:https://www.cnblogs.com/liyihome/p/3669964.html
Copyright © 2020-2023  润新知