华恩JAVA班第13天

xiaoxiao2021-02-28  87

华恩JAVA班第13天

一、构造代码块:

public class Test {  public static void main(String[] args)  {   Person A = new Person();   Person B = newPerson("小李");   System.out.println(A.coutry);   System.out.println(B.coutry);  } } class Person{  String name;  String coutry;  Person(){   System.out.println("我是无参构造方法");  }  Person(String name){   this.name = name;   System.out.println("我是有参构造方法");  }  {  coutry = "中国";  } }

 

二、构造方法之间的调用

public class Test{  public static void main(String[] args){   Student S = newStudent("zjj");   Student B = newStudent("zxc",15);  } } class Student{  String name;  int age;  Student(){   System.out.println("无参构造方法");  }  Student(String name){   this();   this.name = name;   System.out.println("abcd");  }  Student(String name,int age){   this(name);   this.age = age;  } }

转载请注明原文地址: https://www.6miu.com/read-73732.html

最新回复(0)