案例程序分析4:
public class test {
public static void main(String[] args) {
Student student = new Student();
System.out.println(student);
System.out.println("--------------------------");
Student student1 = new Student();
System.out.println(student1);
System.out.println("--------------------------");
Student student2 = new Student("李易峰", 32);
System.out.println(student2);
System.out.println("--------------------------");
Student student3 = new Student();
student3.setName("尼古拉斯赵四");
student3.setAge(48);
System.out.println(student3.getName());
System.out.println(student3.getAge());
System.out.println("--------------------------");
Student student4 = new Student("胡歌", 36);
System.out.println(student4.getName());
System.out.println(student4.getAge());
}
}
class Student{
private String name;
private int age;
public Student(){
System.out.println("空参的构造方法调用了");
}
public Student(String name){
System.out.println("一个参数的构造方法调用了"+name);
}
public Student(String name,int age){
System.out.println("两个参数的构造方法调用了"+name+age);
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
案例程序分析5:
public class Teacher {
private String name;
private int age;
//空参构造手动写出
public Teacher(){
System.out.println("空参构造调用了");
}
public Teacher(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
--------------------------------------------
public class test {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setName("杨超越");
teacher.setAge(21);
System.out.println(teacher.getName());
System.out.println(teacher.getAge());
System.out.println("--------------------");
Teacher teacher1 = new Teacher("刘亦菲",34);
System.out.println(teacher1.getName());
System.out.println(teacher1.getAge());
}
}
案例程序分析6:
public class Phone {
private String name;
private double price;
public Phone() {
}
//快速生成构造方法 alt+insert 选constructor
public Phone(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void call(){
System.out.println("打电话");
}
public void sendweixin(){
System.out.println("发微信");
}
}
--------------------------------------------------
public class test {
public static void main(String[] args) {
Phone phone = new Phone();
phone.setName("华为P30");
phone.setPrice(3000);
phone.call();
phone.sendweixin();
System.out.println(phone.getName());
System.out.println(phone.getPrice());
System.out.println("-----------------------");
Phone phone1 = new Phone("小米",1500);
phone1.call();
phone1.sendweixin();
System.out.println(phone1.getName());
System.out.println(phone1.getPrice());
}
}