菜鸟中的菜鸟求助一道关于java继承的题目
实现一个超类person,制作两个类studentinstructor,这两个类可以从超类继承。一个person有名字和出生年份,一个student有主修科目(major...
实现一个超类person,制作两个类student instructor,这两个类可以从超类继承。一个person有名字和出生年份,一个student有主修科目(major),一个instructor有收入(salary). 为每一个类写一个构造器并且写一个分类定义。 并且写一个测试程序来测试每一个类和方法。
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from
Person. A person has a name and a year of birth. A student has a major, and an instructor has
a salary. Write the class definitions and the constructors for all classes. Supply a test program
that tests these classes and methods. 展开
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from
Person. A person has a name and a year of birth. A student has a major, and an instructor has
a salary. Write the class definitions and the constructors for all classes. Supply a test program
that tests these classes and methods. 展开
7个回答
展开全部
public class Person {
private String name;
private String birthYear;
Person(String pName, String pBirthYear){
name = pName;
birthYear = pBirthYear;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the birthYear
*/
public String getBirthYear() {
return birthYear;
}
/**
* @param birthYear the birthYear to set
*/
public void setBirthYear(String birthYear) {
this.birthYear = birthYear;
}
}
public class Student extends Person {
private String major;
Student(String pName, String pBirthYear, String pMajor) {
super(pName, pBirthYear);
major = pMajor;
}
/**
* @return the major
*/
public String getMajor() {
return major;
}
/**
* @param major the major to set
*/
public void setMajor(String major) {
this.major = major;
}
}
public class Instructor extends Person {
private float salary;
Instructor(String pName, String pBirthYear, float pSalary) {
super(pName, pBirthYear);
salary = pSalary;
}
/**
* @return the salary
*/
public float getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(float salary) {
this.salary = salary;
}
}
public class Testor {
/**
* @param args
*/
public static void main(String[] args) {
Person[] p = new Person[3];
p[0] = new Person("P1", "1949");
p[1] = new Student("P2", "1949", "1");
p[2] = new Instructor("P3", "1949", 2);
for (Person i : p) {
System.out.println("----------------------");
System.out.println("Name: " + i.getName());
System.out.println("BirthYear: " + i.getBirthYear());
if (i instanceof Student) {
System.out.println("Major: " + ((Student) i).getMajor());
}
if (i instanceof Instructor) {
System.out.println("Salary: " + ((Instructor) i).getSalary());
}
}
}
}
private String name;
private String birthYear;
Person(String pName, String pBirthYear){
name = pName;
birthYear = pBirthYear;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the birthYear
*/
public String getBirthYear() {
return birthYear;
}
/**
* @param birthYear the birthYear to set
*/
public void setBirthYear(String birthYear) {
this.birthYear = birthYear;
}
}
public class Student extends Person {
private String major;
Student(String pName, String pBirthYear, String pMajor) {
super(pName, pBirthYear);
major = pMajor;
}
/**
* @return the major
*/
public String getMajor() {
return major;
}
/**
* @param major the major to set
*/
public void setMajor(String major) {
this.major = major;
}
}
public class Instructor extends Person {
private float salary;
Instructor(String pName, String pBirthYear, float pSalary) {
super(pName, pBirthYear);
salary = pSalary;
}
/**
* @return the salary
*/
public float getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(float salary) {
this.salary = salary;
}
}
public class Testor {
/**
* @param args
*/
public static void main(String[] args) {
Person[] p = new Person[3];
p[0] = new Person("P1", "1949");
p[1] = new Student("P2", "1949", "1");
p[2] = new Instructor("P3", "1949", 2);
for (Person i : p) {
System.out.println("----------------------");
System.out.println("Name: " + i.getName());
System.out.println("BirthYear: " + i.getBirthYear());
if (i instanceof Student) {
System.out.println("Major: " + ((Student) i).getMajor());
}
if (i instanceof Instructor) {
System.out.println("Salary: " + ((Instructor) i).getSalary());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Person {
private String name;
private String birthYear;
Person(String pName, String pBirthYear){
name = pName;
birthYear = pBirthYear;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setBirthYear(String birthYear) {
this.birthYear = birthYear;
}
public String getBirthYear() {
return birthYear;
}
}
public class Student extends Person {
private String major;
Student(String pName, String pBirthYear, String pMajor) {
super(pName, pBirthYear);
major = pMajor;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
public class Instructor extends Person {
private float salary;
Instructor(String pName, String pBirthYear, float pSalary) {
super(pName, pBirthYear);
salary = pSalary;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
public class Testor {
public static void main(String[] args) {
Person[] p = new Person[3];
p[0] = new Person("P1", "1949");
p[1] = new Student("P2", "1949", "1");
p[2] = new Instructor("P3", "1949", 2);
for (Person i : p) {
System.out.println("Name: " + i.getName());
System.out.println("BirthYear: " + i.getBirthYear());
if (i instanceof Student) {
System.out.println("Major: " + ((Student) i).getMajor());
}
if (i instanceof Instructor) {
System.out.println("Salary: " + ((Instructor) i).getSalary());
}
}
}
}
private String name;
private String birthYear;
Person(String pName, String pBirthYear){
name = pName;
birthYear = pBirthYear;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setBirthYear(String birthYear) {
this.birthYear = birthYear;
}
public String getBirthYear() {
return birthYear;
}
}
public class Student extends Person {
private String major;
Student(String pName, String pBirthYear, String pMajor) {
super(pName, pBirthYear);
major = pMajor;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
public class Instructor extends Person {
private float salary;
Instructor(String pName, String pBirthYear, float pSalary) {
super(pName, pBirthYear);
salary = pSalary;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
public class Testor {
public static void main(String[] args) {
Person[] p = new Person[3];
p[0] = new Person("P1", "1949");
p[1] = new Student("P2", "1949", "1");
p[2] = new Instructor("P3", "1949", 2);
for (Person i : p) {
System.out.println("Name: " + i.getName());
System.out.println("BirthYear: " + i.getBirthYear());
if (i instanceof Student) {
System.out.println("Major: " + ((Student) i).getMajor());
}
if (i instanceof Instructor) {
System.out.println("Salary: " + ((Instructor) i).getSalary());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Date;
public class Test{
public static void main(String[] args){
Person me=new Person("Michael Jackson","1958/8/29");
Instrutor elvis=new Instrutor("Elvis Preslay","1935/1/8",1);
Student declan=new Student("Declan Galbraith","1991/12/19","Music");
System.out.println(me.getDescription());
System.out.println(elvis.getDescription());
System.out.println(declan.getDescription());
}
}
class Person{
String name;
Date birthday;
Person(String name,String birthday){
this.name=name;
this.Dat=new Date(birthday);
}
String getDescription(){
return String.format("He was born as %1$/%2$/%3$. He is %4$. His name is %5$.",birthday.getYear(),birthday.getMonth(),birthday.getDate(),new Date().getYear()-birthday.getYear(),name);
}
}
class Instructor extends Person{
double salary;
Instructor(String name,String birthday,double salary){
super(name,birthday);
this.salary=salary;
}
String getDescription(){
return String.format("%1$ His salary is %2$.",super.getDescription(),salary);
}
}
class Student extends Person{
String major;
Student(String name,String birthday,String major){
super(name,birthday);
this.major=major;
}
String getDescription(){
return String.format("%1$ The major is %2$.",super.getDescription(),major);
}
}
public class Test{
public static void main(String[] args){
Person me=new Person("Michael Jackson","1958/8/29");
Instrutor elvis=new Instrutor("Elvis Preslay","1935/1/8",1);
Student declan=new Student("Declan Galbraith","1991/12/19","Music");
System.out.println(me.getDescription());
System.out.println(elvis.getDescription());
System.out.println(declan.getDescription());
}
}
class Person{
String name;
Date birthday;
Person(String name,String birthday){
this.name=name;
this.Dat=new Date(birthday);
}
String getDescription(){
return String.format("He was born as %1$/%2$/%3$. He is %4$. His name is %5$.",birthday.getYear(),birthday.getMonth(),birthday.getDate(),new Date().getYear()-birthday.getYear(),name);
}
}
class Instructor extends Person{
double salary;
Instructor(String name,String birthday,double salary){
super(name,birthday);
this.salary=salary;
}
String getDescription(){
return String.format("%1$ His salary is %2$.",super.getDescription(),salary);
}
}
class Student extends Person{
String major;
Student(String name,String birthday,String major){
super(name,birthday);
this.major=major;
}
String getDescription(){
return String.format("%1$ The major is %2$.",super.getDescription(),major);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我写了一个,不知道符不符合你的要求
class Person {
String name;
String birth;
Person(String n,String b){
name = n;
birth =b;
}
public void getName() {
System.out.println("name=" + name);
}
public void getBirth(){
System.out.println("birth=" + birth);
}
}
class Student extends Person{
String major;
Student(String n,String b,String m){
super(n,b);
major = m;
}
public void getMajor(){
System.out.println("major=" + major);
}
}
class Instructor extends Person {
double salary;
Instructor(String n,String b,double s) {
super(n,b);
salary = s;
}
public void getSalary() {
System.out.println("salary=" + salary);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student("xiao li","1990-05-18","math");
Instructor i = new Instructor("lao wang","1980-11-24",3425.78);
System.out.println("Student Information:");
s.getName();
s.getBirth();
s.getMajor();
System.out.println("Instructor Information:");
i.getName();
i.getBirth();
i.getSalary();
}
}
class Person {
String name;
String birth;
Person(String n,String b){
name = n;
birth =b;
}
public void getName() {
System.out.println("name=" + name);
}
public void getBirth(){
System.out.println("birth=" + birth);
}
}
class Student extends Person{
String major;
Student(String n,String b,String m){
super(n,b);
major = m;
}
public void getMajor(){
System.out.println("major=" + major);
}
}
class Instructor extends Person {
double salary;
Instructor(String n,String b,double s) {
super(n,b);
salary = s;
}
public void getSalary() {
System.out.println("salary=" + salary);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student("xiao li","1990-05-18","math");
Instructor i = new Instructor("lao wang","1980-11-24",3425.78);
System.out.println("Student Information:");
s.getName();
s.getBirth();
s.getMajor();
System.out.println("Instructor Information:");
i.getName();
i.getBirth();
i.getSalary();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你已经是菜鸟中的精英了,哈哈,我都看不懂那是什么。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询