オブジェクト向け練習問題--継承
4567 ワード
1、必要に応じて、以下のコード(継承のみ、抽象クラスなし、標準フォーマットで書く)を完成し、テストクラスでテストを行う.需要一:プロジェクトマネージャー類属性:氏名労働番号給与ボーナス行為:ワークワークワーク(氏名、労働番号、給与、ボーナスを印刷する)プログラマー類属性:氏名労働番号給与行為:ワークワークワーク(氏名、労働番号、給与を印刷する)
/**プログラマー類属性:氏名工号給与行為:ワークワークワーク(氏名、工号、給与の印刷) @author dell
*/public class Programer { private String name ; private int num ; private double salary ; public Programer(String name,int num,double salary ) { this.name = name; this.num = num; this.salary = salary; } public double getSary() { return salary; } public String getName() { return name; } public int getNum() { return num; } }
/**プロジェクトマネージャ類属性:氏名労働番号給与ボーナス行為:ワークワークワーク(氏名、労働番号、給与、ボーナスの印刷) @author dell
*/public class Manager extends Programer{private double bonus;//ボーナスpublic Manager(String name,int mum,double salary){super(name,mum,salary);bonus=0;}public double getSalary() { double baseSalary = super.getSary(); return baseSalary + bonus; } public void setBonus(double a) { bonus = a; }
}
/**需要二:既知の学生類と先生類は以下の通り:行為:食事の属性:名前、年齢の先生は特有の方法があります:授業の学生は特有の方法があります:学習 @author dell
*/public class Student1 { private String name; private int age;
}
/**需要二:既知の学生類と先生類は以下の通り:行為:食事の属性:名前、年齢の先生は特有の方法があります:授業の学生は特有の方法があります:学習 @author dell
*/public class Teacher extends Student1 {
}
/**需要三:既知の猫類と犬類:属性:毛の色、足の個数行為:食事猫特有行為:ネズミを捕まえるcatch Mouse犬特有行為:留守番lookHome @author dell
*/public class Cat {
}
/**需要三:既知の猫類と犬類:属性:毛の色、足の個数行為:食事猫特有行為:ネズミを捕まえるcatch Mouse犬特有行為:留守番lookHome @author dell
*/public class Dog extends Cat {
}
/**テストクラス @author dell
*/public class ManagerTest {
}
/**
*/public class Programer { private String name ; private int num ; private double salary ; public Programer(String name,int num,double salary ) { this.name = name; this.num = num; this.salary = salary; } public double getSary() { return salary; } public String getName() { return name; } public int getNum() { return num; } }
/**
*/public class Manager extends Programer{private double bonus;//ボーナスpublic Manager(String name,int mum,double salary){super(name,mum,salary);bonus=0;}public double getSalary() { double baseSalary = super.getSary(); return baseSalary + bonus; } public void setBonus(double a) { bonus = a; }
}
/**
*/public class Student1 { private String name; private int age;
public Student1(String name,int age)
{
this.name = name;
this.age = age;
}
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 void Study()
{
System.out.println(" ");
}
public void eat()
{
System.out.println(" ");
}
}
/**
*/public class Teacher extends Student1 {
public Teacher(String name, int age) {
super(name, age);
}
public void Teach()
{
System.out.println(" , , , ");
}
public void eat()
{
System.out.println(" ");
}
}
/**
*/public class Cat {
@SuppressWarnings("unused")
private String haircolor;
@SuppressWarnings("unused")
private int legnum;
public String getHaircolor() {
return haircolor;
}
public void setHaircolor(String haircolor) {
this.haircolor = haircolor;
}
public int getLegnum() {
return legnum;
}
public void setLegnum(int legnum) {
this.legnum = legnum;
}
public Cat(String haircolor,int legnum)
{
this.setHaircolor(haircolor);
this.setLegnum(legnum);
}
public void catchMouse()
{
System.out.println("catchmouse everyday");
}
}
/**
*/public class Dog extends Cat {
public Dog(String haircolor, int legnum) {
super(haircolor, legnum);
// TODO Auto-generated constructor stub
}
public void lookHome()
{
System.out.println(" ");
}
}
/**
*/public class ManagerTest {
public static void main(String[] args) {
//
Manager boss = new Manager("Austin", 101, 80000);
boss.setBonus(5000);
System.out.println("name:"+ boss.getName()+" " + " :" +boss.getNum()+" "+"salary="+ boss.getSalary());
Programer L = new Programer(" ",202,20000);
System.out.println("name:" + L.getName()+" " + " :"+ L.getNum()+" " +"salary="+ L.getSary());
System.out.println();
//
Teacher H = new Teacher("Austin", 42);
System.out.println(" :" + H.getName() + " " + " :" + H.getAge() );
System.out.print(" :" + " ");
H.Teach();
System.out.print(" :" + " ");
H.eat();
Student1 s = new Student1("Brain", 21);
System.out.println(" :" + s.getName() +" " + " :" + s.getAge() );
System.out.print(" :" + " ");
s.Study();
System.out.print(" :" + " ");
s.eat();
System.out.println();
//
Cat q = new Cat(" ",4);
q.catchMouse();
System.out.println(q.getHaircolor()+ ", " + q.getLegnum() + " ");
Dog p = new Dog(" ",4);
p.lookHome();
System.out.println(p.getHaircolor()+ ", " + p.getLegnum() + " ");
}
}