首页 > 代码库 > java继承3个小题

java继承3个小题

1、实现一个名为Person的类和它的子类EmployeeEmployee有两个子类FacultyStaff具体要求如下:

1Person类中的属性有:姓名nameString类型),地址addressString类型),电话号码telphoneString类型)和电子邮件地址emailString类型)

2Employee类中的属性有:办公室officeString类型),工资wagedouble类型),受雇日期hiredateString类型)

3Faculty类中的属性有:学位degreeString类型),级别levelString类型)

4Staff类中的属性有:职务称号dutyString类型)。

 

public class Person {    private String name;    private String address;    private String telphone;    private String email;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public String getTelphone() {        return telphone;    }    public void setTelphone(String telphone) {        this.telphone = telphone;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    }

 

public class Employee extends Person {    private String office;    private double wage;    private String hiredate;    public String getOffice() {        return office;    }    public void setOffice(String office) {        this.office = office;    }    public double getWage() {        return wage;    }    public void setWage(double wage) {        this.wage = wage;    }    public String getHiredate() {        return hiredate;    }    public void setHiredate(String hiredate) {        this.hiredate = hiredate;    }    }
public class Faculty extends Person{    private String degree;    private String level;    public String getDegree() {        return degree;    }    public void setDegree(String degree) {        this.degree = degree;    }    public String getLevel() {        return level;    }    public void setLevel(String level) {        this.level = level;    }    }

测试:

public class F {    public static void main(String[] args) {        Faculty a=new Faculty();        a.setName("张三");        a.setTelphone("123456788");        a.setDegree("本科");        a.setLevel("一级");        a.setEmail("12345877@qq.com");        System.out.println("员工姓名:"+a.getName()+" 电话"+a.getTelphone()+                " 学位"+a.getDegree()+" 级别"+a.getLevel()+" 邮箱"+a.getEmail());        Employee b=new Employee();        b.setName("张三");        b.setTelphone("123456788");        b.setEmail("12345877@qq.com");        b.setOffice("A座407");        b.setWage(20000);        System.out.println("员工姓名:"+b.getName()+" 电话"+b.getTelphone()+                " 邮箱"+a.getEmail()+" 办公室"+b.getOffice()+" 工资"+b.getWage());    }}

技术分享

2、编写一个Car类,具有String类型的属性品牌,具有功能drive定义其子类AodiBenchi,具有属性:价格、型号;具有功能:变速;定义主类E,在其main方法中分别创建AodiBenchi的对象并测试对象的特性。

public class Car {    private String pinpa;                  //品牌属性    private double sudu;        public String getPinpa() {        return pinpa;    }    public void setPinpa(String pinpa) {        this.pinpa = pinpa;    }    public double getSudu() {        return sudu;    }    public void setSudu(double sudu) {        this.sudu = sudu;    }    public String drive(){        return "汽车已启动!";    }    }
public class Aodi extends Car{    private double jiage;    private String xinghao;        public double getJiage() {        return jiage;    }    public void setJiage(double jiage) {        this.jiage = jiage;    }    public String getXinghao() {        return xinghao;    }    public void setXinghao(String xinghao) {        this.xinghao = xinghao;    }    public double biansu(){        double sudu=super.getSudu()+10;        return sudu;    }}
public class Benchi extends Aodi{    public double biansu(){        double sudu=super.getSudu()+20;        return sudu;    }}

测试:

public class E {    public static void main(String[] args){        Aodi aodi=new Aodi();        aodi.setJiage(500000);        aodi.setXinghao("奥迪A6");        System.out.println("奥迪车的型号是:"+aodi.getXinghao()+" 价格是:"+aodi.getJiage());        System.out.println(aodi.drive());        System.out.println("启动后的速度为:"+aodi.biansu());        Benchi benchi=new Benchi();        benchi.setJiage(800000);        benchi.setXinghao("奔驰2000");        System.out.println("奔驰车的型号是:"+benchi.getXinghao()+" 价格是:"+benchi.getJiage());        System.out.println(benchi.drive());        System.out.println("启动后的速度为:"+benchi.biansu());            }}

技术分享

3、按要求编写一个Java应用程序:

1)编写一个矩形类Rect,包含:

两个属性:矩形的宽width;矩形的高height

两个构造方法:

1.一个带有两个参数的构造方法,用于将widthheight属性初化;

2.一个不带参数的构造方法,将矩形初始化为宽和高都为10

两个方法:

求矩形面积的方法area()

求矩形周长的方法perimeter()

2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用

矩形的左上角坐标来标识,包含:

添加两个属性:矩形左上角坐标startXstartY

两个构造方法:

4个参数的构造方法,用于对startXstartYwidthheight属性初始化;

不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0的矩形;

添加一个方法:

判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩形内,返回true, 否则,返回false

 提示:点在矩形类是指满足条件:x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)

3)编写PlainRect类的测试程序创建一个左上角坐标为(1010),长为20,宽为10的矩形对象;计算并打印输出矩形的面积和周长;判断点(25.513)是否在矩形内,并打印输出相关信息。

public class Rect {    //声明属性    private double width;    private double height;    //带参数构造方法    public Rect(double width,double height){        this.width=width;        this.height=height;    }    //无参的构造方法    public Rect(){        this.width=10;        this.height=10;    }    //成员方法       周长    public double perimeter(){        double perimeter;        perimeter=(width+height)*2;        return perimeter;    }    //成员方法      面积    public double area(){        double area;        area=width*height;        return area;    }    //getter/setter方法    public double getWidth() {        return width;    }    public void setWidth(double width) {        this.width = width;    }    public double getHeight() {        return height;    }    public void setHeight(double height) {        this.height = height;    }}
public class PlainRect extends Rect {    //声明属性    private int startX;    private int startY;    //含参构造方法    public PlainRect(int startX,int startY,double width,double height){        super(width,height);                  //调用父类构造方法        this.startX=startX;        this.startY=startY;    }    //不含参构造方法    public PlainRect(){        this.startX=0;        this.startY=0;    }    //成员方法  判断点与矩形位置    public boolean isInside(double x,double y){        if(x>=startX&&x<=(startX+getWidth())&&y<startY&&y>=(startY-getHeight())){            return true;        }        else{            return false;        }    }    }
public class TestPlainRect {    public static void main(String[] args) {        //创建矩形        PlainRect rect =new PlainRect(10,10,20,10);        //打印输出        System.out.println("长为20,宽为10的矩形的面积是:"+rect.area());        System.out.println("长为20,宽为10的矩形的周长是:"+rect.perimeter());        if(rect.isInside(25.5, 13)==true){            System.out.println("点(25.5,13)在矩形内");        }        else{            System.out.println("点(25.5,13)不在矩形内");        }    }}

技术分享

 

java继承3个小题