创建了一个父类,想在子类中给父类的属性传值,现在是代码运行后,显示的是默认值
public class Message {
private String name;
private String sex;
private int age;
private int phone;
public Message(String name,String sex , int age,int phone){
this.name=name;
this.age=age;
this.phone=phone;
this.sex=sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public Message(){}
}
import java.util.*;
public class Method extends Message {
public Method(String name,String sex , int age,int phone){
super(name,sex,age,phone);
}
public Method(){}
public static void muLu(){
Scanner sc= new Scanner(System.in);
while (true) {
System.out.println("--------客户列表-------");
System.out.println("1.添加客户信息");
System.out.println("2.查询客户信息");
System.out.println("3.退出系统");
System.out.print("请输入选项:\t");
int ku = sc.nextInt();
Message met = new Message();
switch (ku) {
case 1:
System.out.println("请录入姓名");
String xm = sc.next();
met.setName(xm);
System.out.println("请录入性别");
String xb = sc.next();
met.setSex(xb);
System.out.println("请录入年龄");
int nl = sc.nextInt();
met.setAge(nl);
System.out.println("请录入手机号");
int sj = sc.nextInt();
met.setPhone(sj);
System.out.println("添加客户成功");
break;
case 2:
System.out.println("--------客户列表-------");
System.out.println("姓名\t" + "性别\t" + "年龄\t" + "电话");
System.out.println(met.getName() + "\t" + met.getSex() + "\t"
+ met.getAge() + "\t" + met.getPhone() );
break;
}
}
}