Java练习题14.2 继承
关注公众号"野心与家",回复"11.19"获取源文件
一、设计一个学生类Student和它的一个子类Undergraduate,要求如下:
1、Student类有name(姓名)和age(年龄)属性,一个包含两个参数的构造方法,用于给name和age属性赋值,一个show方法打印Student的属性信息。
2、本科生类Undergraduate增加一个speciality(专业)属性。有一个包含三个参数的构造方法,前两个参数用于给继承的name和age属性赋值,第三个参数给speciality专业赋值,一个show方法用于打印Undergraduate的属性信息。
3、在测试类中分别创建Student和Undergraduate对象,调用它们的show方法。
package com.shangjiti.aoian;
public class No1 {
public static void main(String[] args) {
Student s=new Student("张三",18);
Undergraduate u=new Undergraduate("李四",25,"计算机软件");
s.show();
u.show();
}
}
class Student{
String name;
int age;
public Student() {
}
public Student(String xm,int nl) {
this.name=xm;
this.age=nl;
}
public void show() {
System.out.println("我是一名学生,我叫"+name+",我今年"+age+"岁");
}
}
class Undergraduate extends Student{
private String speciality;
public Undergraduate(){
}
public Undergraduate(String xm,int nl,String zy){
super(xm,nl);
this.speciality=zy;
}
public void show() {
System.out.println("我是一名本科生,我叫"+name+",我今年"+age+"岁,我的专业是"+speciality);
}
}
二、按要求编写一个Java应用程序:
1、编写一个矩形类Rectangle,包含:
(1)两个属性:矩形的宽width;矩形的高height。
(2)两个构造方法:
一个带有两个参数的构造方法,用于将width和height属性初化;
一个不带参数的构造方法,将矩形初始化为宽和高都为10。
(3)两个方法:
求矩形面积的方法area()
求矩形周长的方法perimeter()
2、通过继承Rectangle类编写一个具有确定位置的矩形类PlainRect,其确定位置用矩形的左上角坐标来标识,包含:
(1)添加两个属性:矩形左上角坐标startX和startY。
(2)两个构造方法:
带4个参数的构造方法,用于对startX、startY、width和height属性初始化;
不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0的矩形;
(3)添加一个方法:
判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩形内,返回true, 否则,返回false。
3、编写PlainRect类的测试程序
(1)创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;
(2)计算并打印输出矩形的面积和周长;
(3)判断点(25.5,13)是否在矩形内,并打印输出相关信息。
package com.shangjiti.aoian;
public class No2 {
public static void main(String[] args) {
PlainRect r = new PlainRect(20,10,10,10);
r.area(20,10);
r.perimeter(20, 10);
r.isInside(25.5, 13);
}
}
class Rectangle{
double width;
double height;
public Rectangle() {
this(10,10);
// width=10;
// height=10;
}
public Rectangle(double width,double height) {
this.width=width;
this.height=height;
}
public void area(double width,double height) {
double area;
area=width*height;
System.out.println("面积:"+area);
}
public void perimeter(double width,double height) {
double per;
per=width*2+height*2;
System.out.println("周长:"+per);
}
}
class PlainRect extends Rectangle{
double startX;
double startY;
public PlainRect(double width,double height,double startX,double startY){
super(width,height);
this.startX=startX;
this.startY=startY;
}
public PlainRect(){
this(0,0,0,0);
// 或 super();
// this.startX=0;
// this.startY=0;
}
public Boolean isInside(double x,double y) {
if(x>=startX&&x<=(startX+width)&&y<=startY&&y>(startY-height)){
System.out.println("该点在矩形内");
return true;
}
else {
System.out.println("该点不在矩形内");
return false;
}
}
}
一、定义两个类Monkey类和People类。要求:
1、 Monkey类中有一个public void speak()方法,在speak方法中输出“咿呀咿呀。。。”的信息。
2、People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。
3、在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。
4、在测试类的main方法中创建Monkey与People类的对象类测试这2个类的功能。
package com.shangjiti.aoian;
public class No3 {
public static void main(String[] args) {
Monkey m=new Monkey();
People p=new People();
m.speak();
p.speak();
p.think();
}
}
class Monkey{
public void speak() {
System.out.println("咿呀咿呀。。。");
}
}
class People extends Monkey{
public void speak() {
System.out.println("小样的,不错嘛!会说话了!");
}
public void think() {
System.out.println("别说话!认真思考!");
}
}
二、按要求创建类:
1、创建一个银行普通账户类Account:
(1)包含属性:
账户名、密码、余额。
(2)包含的方法:
一个带3个参数的开户方法Account(用户名,账户密码,开户金额),开户的同时设置账户名、密码和余额;
一个带两个参数(密码和交易额)的取钱方法withdraw();
一个带一个参数(交易额)的存钱方法deposit();
要求每次存取款之后显示操作信息和余额。
2、创建一个VipAccount类:
VipAccount账户多了一个属性:卡号,卡号是在10000-99999之间;
除此之外,VipAccount账户取钱时最多可以透支2000元(包含2000)。
package com.shangjiti.aoian;
public class No4 {
public static void main(String[] args) {
VipAccount vip=new VipAccount("张三","123",990,77177);
vip.deposit(10000.0);
vip.withdraw("123", 12000.0);
}
}
class Account{
String name;
String password;
double balance;
public Account() {
}
public Account(String xm,String mm,double m) {
this.name=xm;
this.balance=m;
this.password=mm;
}
public void deposit(double money)
{
balance=balance+money;
System.out.println("您好,您的账户已存入"+money+"元,您的账户余额:"+balance+"元。");
}
public void withdraw(String mm,double money)
{
if(this.password==mm)
{
if(this.balance>=money)
{
this.balance=this.balance-money;
System.out.println("您好,您的账户已取出"+money+"元,您的账户余额:"+this.balance+"元。");
}
else {
balance=balance-money;
if(balance>=-2000) {
System.out.println("您好,您的账户已取出"+money+"元,您的账户透支:"+this.balance+"元。");
System.out.println("您的账户余额不足!");
}
else
System.out.println("您的账户透支额度不足!");
}
}
else
System.out.println("密码错误");
}
}
class VipAccount extends Account{
int id;
public VipAccount() {
}
public VipAccount(String xm, String mm, double m,int id) {
super(xm,mm,m);
this.id=id;
System.out.println("Vip账户----------");
System.out.println("开户成功 ! 卡号:"+id+",余额:"+balance);
}
public void deposit(double money) {
super.deposit(money);
}
public void withdraw(String mm,double money) {
super.withdraw(mm, money);
}
}