java基础4--面向对象下(几道练习题)

一、

/*定义一个ClassName接口,接口中只有一个抽象方法getClassName();设计一个
  类Company。该类实现接口ClassName中的方法getClassName()。功能是获取该类的
  名称;编写应用程序使用Company类*/

 

 

interface ClassName{ String getClassName(); } class Company implements ClassName{ public String getClassName(){ return "Company"; } } public class Test6_1{ public static void main(String[] args){ System.out.println(new Company().getClassName()); } }

 

 

二、

/*建立一个人类Person和学生类Student。功能要求如下:
 (1)Person中包含4个私有的数据成员name、addr、sex、age。分别为字符串型、字符型和整型。
   用一个4参构造方法、一个2参构造方法、一个无参构造方法、一个输出方法显示4种属性。
 (2)Student类继承Person类、并增加成员math、english存放数学和英语成绩。一个6参构造方法、
      一个2参构造方法、一个无参构造方法并重写输出方法用于显示6种属性。*/
class Person{ private String name; private String addr; private char sex; private int age; public void setName(String name){ this.name = name; } public void setAddr(String addr){ this.addr = addr; } public void setSex(char sex){ this.sex = sex; } public void setAge(int age){ this.age = age; } public Person(String name,String addr,char sex,int age){ this.setName(name); this.setAddr(addr); this.setSex(sex); this.setAge(age); } public Person(String name,int age){ this.setName(name); this.setAge(age); } public Person(){} public String getInfo(){ return "name: "+this.name+ "; addr: "+this.addr+ "; sex: "+this.sex+ "; age: "+this.age; } public void say(){ System.out.println(this.getInfo()); } } class Student extends Person{ private int math; private int english; public void setMath(int math){ this.math = math; } public void setEnglish(int english){ this.english = english; } public Student(String name,String addr,char sex,int age,int math,int english){ super(name,addr,sex,age); this.math = math; this.english = english; } public Student(String name,int age){ super(name,age); } public Student(){}; public String getInfo(){ return super.getInfo()+ "; math: "+this.math+ "; english: "+this.english; } public void say(){ super.say(); } } public class Test6_3{ public static void main(String[] args){ Person per1 = new Person("lisi",27); Person stu1 = new Student("zhangsan","huiyang",'M',20,68,91); Student stu2= new Student("hxq","huizhou",'M',20,98,82); per1.say(); stu1.say(); stu2.say(); } }

 

 

/*定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。定义
  管理层类,继承员工类,并有自己的属性职务和年薪。*/
abstract class Employee{ private String name; private int age; private String sex; public Employee(){} public Employee(String name,int age,String sex){ this.setName(name); this.setAge(age); this.setSex(sex); } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public void setSex(String sex){ this.sex = sex; } public String getInfo(){ return "name = "+this.name+ "; age = "+this.age+ "; sex = "+this.sex; } public abstract void print(); } class Managemen extends Employee{ private String position; private int annualPay; public Managemen(){} public Managemen(String name,int age,String sex){ super(name,age,sex); } public Managemen(String name,int age,String sex,String position,int annualPay){ super(name,age,sex); this.setPosition(position); this.setAnnualPay(annualPay); } public void setPosition(String position){ this.position = position; } public void setAnnualPay(int annualPay){ this.annualPay = annualPay; } public String getInfo(){ return super.getInfo()+ "; position = "+this.position+ "; annualPay = "+this.annualPay; } public void print(){ System.out.println(this.getInfo()); } } public class Test6_4{ public static void main(String[] aegs){ Managemen mgm = new Managemen("hxq",27,"man","manager",100000); mgm.print(); } }

你可能感兴趣的:(java基础4--面向对象下(几道练习题))