新手,明天考试了
刚学这方面的,很多基本的都不会,对这个题目没信心,晚上10点来核对答案,我不是不动手,现在正在做着,只是为保明天考试能通过,麻烦各位给出下面的题目正确答案,谢谢编程:
1. 编写Person类,该类包含:
四个受私有属性:人的姓名name、年龄age、地址address和电话tel。
一个构造器方法:用于初始化name、 age和address属性。
两个公有成员方法:分别实现人基本信息的输出和设置电话。
2.编写学生类Student继承于Person类
增加成员变量学校school、学号id
重写父类输出方法,将学生所有信息输出。
实例化一个对象,并输出其信息。
3.扩展:编写教师类Teacher继承于Person类
增加成员变量学校school、编号id、职称。
搜索更多相关的解决方案:
考试
----------------解决方案--------------------------------------------------------
程序代码:
class Person {
String name;
int age;
String address;
int tel;
}
class Student extends Person{
String school;
int id;
}
class Teacher extends Person{
String school;
int id;
String career;
}
public class test{
public static void main(String args[]){
Student student=new Student();
student.name="张三";
student.age=20;
student.address="北京";
student.tel=888168;
student.school="清华";
student.id=88;
System.out.println(student.name);
System.out.println(student.age);
System.out.println(student.address);
System.out.println(student.tel);
System.out.println(student.school);
System.out.println(student.id);
Teacher teacher=new Teacher();
teacher.name="李四";
teacher.age=29;
teacher.address="北京";
teacher.tel=123456;
teacher.school="清华";
teacher.id=22;
teacher.career="教师";
System.out.println(teacher.name);
System.out.println(teacher.age);
System.out.println(teacher.address);
System.out.println(teacher.tel);
System.out.println(teacher.school);
System.out.println(teacher.id);
System.out.println(teacher.career);
}
}
String name;
int age;
String address;
int tel;
}
class Student extends Person{
String school;
int id;
}
class Teacher extends Person{
String school;
int id;
String career;
}
public class test{
public static void main(String args[]){
Student student=new Student();
student.name="张三";
student.age=20;
student.address="北京";
student.tel=888168;
student.school="清华";
student.id=88;
System.out.println(student.name);
System.out.println(student.age);
System.out.println(student.address);
System.out.println(student.tel);
System.out.println(student.school);
System.out.println(student.id);
Teacher teacher=new Teacher();
teacher.name="李四";
teacher.age=29;
teacher.address="北京";
teacher.tel=123456;
teacher.school="清华";
teacher.id=22;
teacher.career="教师";
System.out.println(teacher.name);
System.out.println(teacher.age);
System.out.println(teacher.address);
System.out.println(teacher.tel);
System.out.println(teacher.school);
System.out.println(teacher.id);
System.out.println(teacher.career);
}
}
----------------解决方案--------------------------------------------------------
程序代码:
class Person {
private String name;
private int age;
private String address;
private int tel;
public Person() {
}
public Person(String name, int age, String address, int tel) {
this.name = name;
this.age = age;
this.address = address;
this.tel = tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getTel() {
return tel;
}
public void setTel(int tel) {
this.tel = tel;
}
@Override
public String toString() {
return "姓名:"+ name + " 年龄:" + age + " 地址:" + address + " 电话:" + tel;
}
}
class Student extends Person {
private String school;
private int id;
public Student() {
super();
}
public Student(String name, int age, String address, int tel,
String school, int id) {
super(name, age, address, tel);
this.school = school;
this.id = id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return super.toString() + " 学校:" + school + " id:" + id;
}
}
class Teacher extends Person {
private String school;
private int id;
private String career;
public Teacher() {
super();
}
public Teacher(String name, int age, String address, int tel,
String school, int id, String career) {
super(name, age, address, tel);
this.school = school;
this.id = id;
this.career = career;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCareer() {
return career;
}
public void setCareer(String career) {
this.career = career;
}
@Override
public String toString() {
return super.toString() + " 学校:" + school + " id:" + id + " 职位:" + career;
}
}
public class Test {
public static void main(String args[]) {
Student student = new Student("张三",20,"北京",888168,"清华",88);
System.out.println(student);
Teacher teacher = new Teacher("李四",29,"北京",123456,"清华",22,"教师");
System.out.println(teacher);
}
}
帮你修改了代码,按JAVABEAN规范写的,祝你考试成功 private String name;
private int age;
private String address;
private int tel;
public Person() {
}
public Person(String name, int age, String address, int tel) {
this.name = name;
this.age = age;
this.address = address;
this.tel = tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getTel() {
return tel;
}
public void setTel(int tel) {
this.tel = tel;
}
@Override
public String toString() {
return "姓名:"+ name + " 年龄:" + age + " 地址:" + address + " 电话:" + tel;
}
}
class Student extends Person {
private String school;
private int id;
public Student() {
super();
}
public Student(String name, int age, String address, int tel,
String school, int id) {
super(name, age, address, tel);
this.school = school;
this.id = id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return super.toString() + " 学校:" + school + " id:" + id;
}
}
class Teacher extends Person {
private String school;
private int id;
private String career;
public Teacher() {
super();
}
public Teacher(String name, int age, String address, int tel,
String school, int id, String career) {
super(name, age, address, tel);
this.school = school;
this.id = id;
this.career = career;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCareer() {
return career;
}
public void setCareer(String career) {
this.career = career;
}
@Override
public String toString() {
return super.toString() + " 学校:" + school + " id:" + id + " 职位:" + career;
}
}
public class Test {
public static void main(String args[]) {
Student student = new Student("张三",20,"北京",888168,"清华",88);
System.out.println(student);
Teacher teacher = new Teacher("李四",29,"北京",123456,"清华",22,"教师");
System.out.println(teacher);
}
}
----------------解决方案--------------------------------------------------------
怎么感觉我也做个这个题啊?
----------------解决方案--------------------------------------------------------