zoukankan      html  css  js  c++  java
  • Java构造方法之间的调用

    package test;
    /*1.构造方法之间的调用使用this来完成。

    this:1.用来区分局部变量和成员变量同名的情况。

    2.在构造方法内就是代本类对象,this代表它所在方法所属对象的引用。

    2.构造函数之间进行调用时,this语句只能出现在第一行,构造方法要先执行,如果构造方

    法中还有初始化,那就执行更细节的初始化。*/
    class Student{

      Student(){
        System.out.println("我是无参数的构造方法");
        }
      Student(String name){
        this();//correct
        System.out.println("我是有参数的构造方法 "+name);
        //this();构造函数调用必须是构造函数中的第一个语句; error
        }
      Student(String name, int age){

        System.out.println("我是有两个参数的构造方法 "+name+age);

        }
      }
    public class Example {
      public static void main(String[] args) {
        Student One = new Student("周瑶瑶",21);

        }
    }

  • 相关阅读:
    140704
    140703
    140702
    堆排序
    并查集
    140701
    这年暑假集训-140630
    vim for python
    hihocode 第九十二周 数论一·Miller-Rabin质数测试
    hdu 3157 Crazy Circuits 有源汇和下界的最小费用流
  • 原文地址:https://www.cnblogs.com/liuguoguo/p/8511605.html
Copyright © 2011-2022 走看看