首页 > 代码库 > Java-继承 共3题

Java-继承 共3题

一、实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。具体要求如下:

(1)Person类中的属性有:姓名name(String类型),地址address(String类型),电话号码telphone(String类型)和电子邮件地址email(String类型);

(2)Employee类中的属性有:办公室office(String类型),工资wage(double类型),受雇日期hiredate(String类型);

(3)Faculty类中的属性有:学位degree(String类型),级别level(String类型);

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

package com.jicheng;public class Person1 {    //属性    private String name;    private String addrees;    private String telphone;    private String email;   
  
public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddrees() { return addrees; } public void setAddrees(String addrees) { this.addrees = addrees; } 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; } }

 

package com.jicheng;public class Employee extends Person1 {    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;    }    }

 

package com.jicheng;public class Faculty extends Employee {    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;    }    private String degree;    private String level;    }

 

package com.jicheng;public class Staff extends Employee{    private String duty;    public String getDuty() {        return duty;    }    public void setDuty(String duty) {        this.duty = duty;    }    }

测试:

package com.jicheng;public class Test02 {    public static void main(String[] args) {        Faculty fa=new Faculty();        fa.setName("李凯");        fa.setTelphone("18353367925");        fa.setDegree("博士");        System.out.println("姓名:"+fa.getName()+"  电话:"+fa.getTelphone()+"  学历:"+fa.getDegree());            Staff st=new Staff();        st.setName("韩如月");        st.setWage(10000);        st.setDuty("主管");        System.out.println("姓名:"+st.getName()+"  工资:"+st.getWage()+"  职位:"+st.getDuty());        }}

结果:

技术分享

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

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()+20;        return sudu;    }}

 

public class Benchi extends Aodi{    public double biansu(){        double sudu=super.getSudu()+30;        return sudu;    }}

测试:

public class E {    public static void main(String[] args){        Aodi aodi=new Aodi();        aodi.setJiage(1000000);        aodi.setXinghao("奥迪");        System.out.println("奥迪车的型号是:"+aodi.getXinghao()+" 价格是:"+aodi.getJiage());        System.out.println(aodi.drive());        System.out.println("速度为:"+aodi.biansu());

三、

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

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

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

两个构造方法:

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

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

两个方法:

求矩形面积的方法area()

求矩形周长的方法perimeter()

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

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

添加两个属性:矩形左上角坐标startX和startY。

两个构造方法:

带4个参数的构造方法,用于对startX、startY、width和height属性

初始化;

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

的矩形;

添加一个方法:

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

形内,返回true, 否则,返回false。

                提示:点在矩形类是指满足条件:

                     x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)

(3)编写PlainRect类的测试程序

创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;

计算并打印输出矩形的面积和周长;

判断点(25.5,13)是否在矩形内,并打印输出相关信息。

package com.jicheng;public class Rect {    //属性    private double width;    private double height;    //有参构造方法    public Rect(double width, double height) {        super();        this.width = width;        this.height = height;    }    //无参构造方法    public Rect() {        super();        this.width=10;        this.height=10;    }    //get  set    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 double area()    {        return this.height*this.width;    }    //周长    public double perimeter()    {        return (this.height+this.width)*2;    }}

 

package com.jicheng;public class PlainRect extends Rect{    private double startX;    private double startY;    //带参数构造    public PlainRect(double width, double height, double startX, double startY) {        super(width, height);        this.startX = startX;        this.startY = startY;    }    //无参构造方法    public PlainRect() {        super(0,0);        this.startX=0;        this.startY=0;    }    public double getStartX() {        return startX;    }    public void setStartX(double startX) {        this.startX = startX;    }    public double getStartY() {        return startY;    }    public void setStartY(double startY) {        this.startY = startY;    }    /***     * 判断是否在矩形内     * @param x 点x坐标     * @param y 点y坐标     * @return    是否在矩形内     */    public Boolean isInside(double x,double y)    {        return x>=startX&&x<=(startX+getWidth())&&y<startY&&y>=(startY-getHeight());    }        }

测试:

package com.jicheng;public class Test03 {    public static void main(String[] args) {        PlainRect pl=new PlainRect(10,20,10,10);        System.out.println("矩形面积="+pl.area());        System.out.println("矩形周长="+pl.perimeter());        System.out.println("测试点是否在矩形内:"+pl.isInside(14, 9));    }}

结果:

技术分享

 

Java-继承 共3题