public class 类名{ 修饰符 类名(参数){ } }
public class Student { private String name; private int age; public Student(){} public Student(String name){ this.name = name; } public Student(int age){ this.age = age; } public void show(){ System.out.println(name + "," + age); } } public class StudentDemo { public static void main(String[] args){ Student s1 = new Student(); s1.show(); Student s2 = new Student(name:"ABC"); s2.show(); Student s3 = new Student(age:30); s3.show(); } }
构造方法的创建
构造方法的重载
推荐的使用方法
成员变量
构造方法
成员方法
创建对象并为成员变量赋值的两种方式
public class Student { private String name; private int age; public Student(){} public Student(String name){ this.name = name; } public Student(int age){ this.age = age; } public void show(){ System.out.println(name + "," + age); } } public class StudentDemo { public static void main(String[] args){ Student s1 = new Student(); s1.show(); Student s2 = new Student(name:"ABC"); s2.show(); Student s3 = new Student(age:30); s3.show(); } }