标签:访问 简单 表示 需要 自动 oid pre 参数 存在
public class Car { public Car() { System.out.println("构造方法"); } public static void main(String[] args){ Car p = new Car(); } }
public class Car { String name; String color; int seat; public Car(String name, String color, int seat) { this.name = name; this.color = color; this.seat = seat; System.out.println("构造了一辆车"); } public static void main(String[] args) { Car p = new Car("大黄蜂", "绿色", 5); System.out.println(p.name + p.color + p.seat); } }
public class DaXia { String name; int age; String bangPai; String nickname; public DaXia(String name, int age, String bangPai) { this.name = name; this.age = age; this.bangPai = bangPai; } public DaXia(String name, int age, String bangPai, String nickname) { this.name = name; this.age = age; this.bangPai = bangPai; this.nickname = nickname; } }
public class DaXia { String name; int age; String bangPai; String nickname; public DaXia(String name, int age, String bangPai) { this.name = name; this.age = age; this.bangPai = bangPai; } public DaXia(String name, int age, String bangPai, String nickname) { this(name, age, bangPai); this.nickname = nickname; System.out.println(this.name + this.age + this.bangPai + this.nickname); } public static void main(String[] args) { DaXia p = new DaXia("乔峰", 23, "少林", "乔帮主"); } }
public class Person { String name; String address; static String country = "大清"; public Person(String name, String address) { this.name = name; this.address = address; } public static void main(String[] args) { Person p1 = new Person("张麻子", "北京四合院"); Person p2 = new Person("黛西", "北京大裤衩"); Person.country = "民国"; System.out.println(p1.country); System.out.println(p2.country); } }
public class Person{ public static void huo(){ System.out.println("是人都想火"); } public static void main(String[] args){ huo(); // 由于是自己类. 可以直接访问静态方法 Person.huo();// 最好呢, 还是用类名去访问. } } 类中内容的加载顺序(简单): public class Person{ { System.out.println("我是通用构造器"); } static { System.out.println("我是静态构造器"); } public Person(){ System.out.println("我是构造方法"); } public static void main(String[] args){ new Person(); } }
标签:访问 简单 表示 需要 自动 oid pre 参数 存在
原文地址:https://www.cnblogs.com/wenyule/p/12192256.html