首页 > 代码库 > 继承0920作业
继承0920作业
第一题:
编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法
中测试类A与类B。
package com.jicheng0920;public class A { public void showA() { char zimu=‘a‘; for(int i=0;i<26;i++) { System.out.print(zimu+" "); if(zimu==‘z‘) { System.out.println(); } zimu++; } }}package com.jicheng0920;public class B extends A { public void showA() { char zimu=‘A‘; for(int i=0;i<26;i++) { System.out.print(zimu+" "); zimu++; } }}package com.jicheng0920;public class C { public static void main(String[] args) { // TODO Auto-generated method stub A a=new A(); a.showA(); B b=new B(); b.showA(); }}
运行结果:
第二题:
实现一个名为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 workhome;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 void p1() { System.out.println("姓名:"+name+", "+"住址:"+address+", "+"电话:"+telphone+", "+"email:"+email); } }package workhome;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 void e() { super.p1();System.out.println("办公室:"+office+", "+"工资:"+wage+", "+"受雇日期:"+hiredate); }}package workhome;public class Faculty extends Employee { 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 void F() { super.e(); System.out.println("学位:"+degree+", "+"级别:"+level); System.out.println(); }}package workhome;public class Staff extends Employee { private String duty;//职务 public String getDuty() { return duty; } public void setDuty(String duty) { this.duty = duty; } public void S() { super.e(); System.out.println("职务:"+duty); System.out.println(); }}
package workhome;
public class testperson0920 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化person类
Person p=new Person();
p.setName("张三");
p.setAddress("淄博张店");
p.setEmail("123456@qq.com");
p.setTelphone("3787110");
p.p1();
//实例化Employee类
Employee E=new Employee();
E.setOffice("汉企401");
E.setHiredate("2016-08-16");
E.setWage(1000);
E.setName("张三");
E.setAddress("淄博张店");
E.setEmail("123456@qq.com");
E.setTelphone("3787110");
E.e();
//实例化Faculty类
Faculty F=new Faculty();
F.setOffice("汉企401");
F.setHiredate("2016-08-16");
F.setWage(1000);
F.setName("张三");
F.setAddress("淄博张店");
F.setEmail("123456@qq.com");
F.setTelphone("3787110");
F.setDegree("博士");
F.setLevel("教育学学士");
F.F();
//实例化Staff类
Staff S=new Staff();
S.setOffice("汉企401");
S.setHiredate("2016-08-16");
S.setWage(1000);
S.setName("张三");
S.setAddress("淄博张店");
S.setEmail("123456@qq.com");
S.setTelphone("3787110");
S.setDuty("教师");
S.S();
}
}
运行结果:
第三题:
编写一个Car类,具有String类型的属性品牌,具有功能drive;
定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特
性。
package workhome;public class Car { //属性 private String look; private String lunzi; public String getLook() { return look; } public void setLook(String look) { this.look = look; } public String getLunzi() { return lunzi; } public void setLunzi(String lunzi) { this.lunzi = lunzi; } //方法 public void drive() { System.out.println(); } }package workhome;public class Aodi extends Car { //属性 private String xh; private double jg; private double sped=100.0; public String getXh() { return xh; } public void setXh(String xh) { this.xh = xh; } public double getJg() { return jg; } public void setJg(double jg) { this.jg = jg; } //方法 public double speed(double i) { sped+=i; return sped; }}package workhome;public class Benchi extends Car { //属性 private String xh; private double jg; private double sped=100; public String getXh() { return xh; } public void setXh(String xh) { this.xh = xh; } public double getJg() { return jg; } public void setJg(double jg) { this.jg = jg; } //方法 public double speed(double i) { sped+=i; return sped; }}
package workhome;
public class E
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
//car
Car c=new Car();
c.setLook("正方形");
c.setLunzi("四轮");
//Aodi
Aodi a=new Aodi();
a.setXh("奥迪A8");
a.setJg(10000000);
System.out.println(a.speed(30.0));
//Benchi
Benchi b=new Benchi();
b.setXh("奔驰s07");
b.setJg(9999999);
System.out.println(b.speed(-20.131));
}
}
运行结果:
第四题:
按要求编写一个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 workhome;public class Rect { //属性 public double width; public double height; //构造方法1 Rect(double width,double height) { this.width=width; this.height=height; } //构造方法2 Rect() { width=10; height=10; } //方法1 public double area() { double mj=width*height; return mj; } //方法2 public double perimeter() { double zc=2*(width+height); return zc; }}package workhome;public class PlainRect extends Rect { private double startX;//矩形左上角坐标 private double startY; 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; } //构造方法1 PlainRect(double startX,double startY,double width,double height) { super(width,height); this.startX=startX; this.startY=startY; } //构造方法2 PlainRect() { super(); startX=0; startY=0; } //方法 public boolean isInside(double x,double y) { if(x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)) { return true; } else { return false; } }}package workhome;public class PlainRecttest { public static void main(String[] args) { // TODO Auto-generated method stub //实例化Rect类 Rect cs1=new Rect(); //实例化PlainRect类 PlainRect cs2=new PlainRect(1,2,20,10); System.out.println(cs2.area()); System.out.println(cs2.perimeter()); System.out.println(cs2.isInside(25.5,13)); }}
运行结果:
继承0920作业