求一道Java编程题!!!

求一道Java编程题!!!编写一个抽象类Person,内有变量姓名name,年龄age,性别sex,有构造方法。定义一个接口Jiekou,接口中有常量国籍country和显示成员变量的printinfo()方法。定义Person类的子类Student,增加变量学校school、专业zhuanye;定义Person类的子类Teacher,增加变量学校school、职称zhicheng、主讲课程teach。类Student和Teacher都使用了该接口。写出一个主类,测试上述类的正确性,测试时使用上转型对象和接口回调技术来体现多态性

第1个回答  2017-04-19
public class Test {// 测试类
public static void main(String[] args) throws IOException {

Student s = new Student("zhangsna",12,"nan","默默大学","机械专业");
Teacher t = new Teacher("lili",30,"nv","默默大学","教授","机械");
s.showInfo();
t.showInfo();
}
}
class People{
String name;
int age;
String sex;
public People(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
}

interface JieKou{
final String COUNTRY="china";
public void showInfo();

}

class Student extends People implements JieKou{
private String school;
private String zhuanye;
@Override
public void showInfo() {
System.out.println("姓名:"+name+"\t年龄:"+age+"性别:"+sex+"学校:"+school+",专业:"+zhuanye);

}
public Student(String name, int age, String sex, String school, String zhuanye) {
super(name, age, sex);
this.school = school;
this.zhuanye = zhuanye;
}

}
class Teacher extends People implements JieKou{
private String school;
private String zhicheng;
private String teach;
@Override
public void showInfo() {
System.out.println("姓名:"+name+"\t年龄:"+age+"性别:"+sex+"学校:"+school+",职称:"+zhicheng+",主讲课程:"+teach);

}
public Teacher(String name, int age, String sex, String school, String zhicheng, String teach) {
super(name, age, sex);
this.school = school;
this.zhicheng = zhicheng;
this.teach = teach;
}

}
就这些
第2个回答  2017-04-19
--接口
public interface DisplayPeople {
public final String country="China";
void printInfo(Person p);
}
--抽线类
public abstract class Person implements DisplayPeople{
private String name;
private int age;
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

public Person(){}
public Person(String name, int age, String sex) {
this();
this.name = name;
this.age = age;
this.sex = sex;
}

}
--Student类
public class Student extends Person implements DisplayPeople{
private String school;
private String profession;
public Student(String school, String profession) {
super();
this.school = school;
this.profession = profession;
}
public Student() {

}
@Override
public void printInfo(Person p) {
// TODO Auto-generated method stub
System.out.println("country:"+DisplayPeople.country+";name:"+p.getName()+";age:"+p.getAge()+";sex:"+p.getSex()+";school:"+this.getSchool()+";profession:"+this.getProfession());
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}

}

--Teacher类
public class Teacher extends Person implements DisplayPeople{
private String school;
private String zhicheng;
private String teach;
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getZhicheng() {
return zhicheng;
}
public void setZhicheng(String zhicheng) {
this.zhicheng = zhicheng;
}
public String getTeach() {
return teach;
}
public void setTeach(String teach) {
this.teach = teach;
}
@Override
public void printInfo(Person p) {
System.out.println("country:"+DisplayPeople.country+";name:"+p.getName()+";age:"+p.getAge()+";sex:"+p.getSex()+";school:"+this.getSchool()+";zhicheng:"+this.getZhicheng()+";teach:"+this.getTeach());

}

}
--测试类
public class Client {

public static void main(String[] args) {
Student student=new Student();
student.setAge(20);
student.setName("John");
student.setSex("Male");
student.setProfession("couputer");
student.setSchool("Beijingdaxue");
Person p=student;
p.printInfo(p);

Teacher tea=new Teacher();
tea.setAge(25);
tea.setName("Mary");
tea.setSex("Female");
tea.setTeach("English");
tea.setSchool("Beijingdaxue");
Person p1=tea;
p.printInfo(p1);
}

}本回答被提问者和网友采纳

Java类的一道题?
下面是实现这道题的 Java 代码:public class Car { \/\/ 品牌 private String brand;\/\/ 颜色 private String color;\/\/ 车龄 private int age;\/\/ 构造方法 public Car() {} \/\/ set 方法 public void setBrand(String brand) { this.brand = brand;} public void setColor(String color) { t...

一道Java编程题,拜托了各位大神
1.定义一个Student类,包括学号,姓名,成绩三个字段,生成get,set和toString方法,实现Comparable接口,重写toCompare方法,方法里就是本题的逻辑,先按成绩比较,再按学好比较,使用TreeSet不实现这个接口会报错。package Collection;public class Student implements Comparable<Student> {private long sno;priv...

一道Java题,求解题详细步骤!下列语句序列执行后,k 的值是。
每个case语句后要加break;不然会从满足条件的那个case开始,一直运行到default.因为j-i=8满足了第一个case,而每个case后面都没有break;所以先执行k++ , 这时k=31 然后执行K+=2, 这时k=33 然后执行K+=3 这时k=36 最后执行k\/j 即36\/18 结果为2 ,如果像下面这样每个case后都有break;那么就是...

用JAVA编程算出《张丘建算经》中的一道百鸡问题
a + b + c = 100 5a + 3b + c\/3 = 100 两方程联立消去c可得到:14a + 8b = 200 也就是说这道题求的是满足 14a + 8b = 200 这个方程的整数解,即 (200 - 14 * a) % 8 == 0 demo(仅供参考):public static void main(String[] args) { \/\/ a,b,c分别是公鸡、母鸡...

一道简单的有关数组的Java程序题,请高手前辈们帮忙查看一下代码,谢谢...
一道简单的有关数组的Java程序题,请高手前辈们帮忙查看一下代码,谢谢! 题目:编写一个工具类,该工具类内包含一个数组属性,可以调用该工具类的add()方法向该工具类中的数组里添加元素,添加的元素可以是任意类型,调用get(下标)方法可以取出某个元素,调... 题目:编写一个工具类,该工具类内包含一个数组属性,可以...

一道Java的题目 求编程
import java.util.Scanner;public class TestSystemIn { public static void main(String[] args) { System.out.println("请输入内容,不同内容以逗号隔开,请以0结尾,以回车结束");Scanner scanner = new Scanner(System.in);String str = scanner.next();String a[] = str.split(",");int ...

一道基本的java题,请基础牢固者进。程序如下:
常量在构造方法赋值的基本用法。因为:INSTANCE是静态变量,不属于任何类,所以可以直接使用"INSTANCE.beltSize()"将要执行它INSTANCE.beltSize()时,静态属性INSTANCE开始初始化,执行第六行的语句“new Elvis2()”执行构造方法Elvis2(),执行Elvis2()时,CURRENT_YEAR的值还为0,所以结果是-1930。

java编程题,在九宫格内填入1—9九个数字,使得横竖排的数字相加之和都...
\/*直接复制运行就可以,每一行的九个数字代表一个九宫格的9个数字,从左到右,从上到下*\/import java.util.ArrayList;import java.util.Arrays;public class Test1 {private static ArrayList<String> arrangeList = new ArrayList<String>(); public static void main(String[] args) {String str ...

一道Java编程题,求各位大神,告知我该怎么写
package javaexc;public class Score { public static void main(String[] args) { \/\/ 定义存储分数数组 int score[][]=new int[20][5]; \/\/总分 int sumscore=0; \/\/存储总分的一维数组 int sumscores[]=new int[20]; \/\/平均分 int avescore=0; System.out.print("core...

一道java编程题,使用for和if实现1-100中能被7整除或者个位数是7的数...
参考代码:public class Demo {public static void main(String[] args) {for (int i=1; i<100; i++) {if (i%7==0 || i%10==7) {System.out.print(i + "\\t");}}System.out.println("");}}

相似回答