【实验十二】
1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。
class Vehicle {
int wheels;//车轮个数
double weight;//车重
public int getwheels() {
return wheels;
}
public void setwheels(int wheels) {
this.wheels=wheels;
}
public double getweight() {
return weight;
}
public void setweight(double weight) {
this.weight=weight;
}
Vehicle(int wheels,double weight){
this.wheels=wheels;
this.weight=weight;
}
}
class Car extends Vehicle{
int loader;//载人数
public int getloader() {
return loader;
}
public void setloader(int lodaer) {
this.loader=lodaer;
}
Car(int wheels,double weight,int loader){
super(wheels,weight);
this.loader=loader;
}
}
class Truck extends Car{
double payloda;//载重
public double getpayloda() {
return payloda;
}
public void setpayloda(double payloda) {
this.payloda=payloda;
}
Truck(int wheels,double weight,int loader,double payloda){
super(wheels,weight,loader);
this.payloda=payloda;
}
}
public class TestVehicle {
public static void main(String args[]) {
Vehicle a = new Vehicle(6,4);
System.out.println("车轮有"+a.getwheels()+"个、车重"+a.getweight()+"吨");
Car b = new Car(1,2,3);
System.out.println("车轮有"+b.getwheels()+"个、车重"+b.getweight()+"吨、能坐"+b.getloader()+"个人");
Truck c = new Truck(4,5,6,7);
System.out.println("车轮有"+c.getwheels()+"个、车重"+c.getweight()+"吨、能坐"
+c.getloader()+"个人、载重"+c.getpayloda()+"吨");
}
}
2.定义一个类,类中有计算体积的两个同名方法,分别可计算圆柱体体积与长方体体积,举例验证程序。
class V {
double h;//高
double l;//长
double s;//宽
double r;//底面半径
public double geth() {
return h;
}
public void seth(double h) {
this.h=h;
}
public double getl() {
return l;
}
public void setl(double l) {
this.l=l;
}
public double gets() {
return s;
}
public void sets(double s) {
this.s=s;
}
public double getr() {
return r;
}
public void setr(double r) {
this.r=r;
}
V(double h,double l,double s){
this.h=h;
this.l=l;
this.s=s;
System.out.println("长方体的体积是"+h*l*s);
}
V(double r,double h){
this.r=r;
this.h=h;
System.out.println("圆柱的体积是"+r*r*h*3.14);
}
}
public class TestV {
public static void main(String args[]) {
V c = new V(1,2,3);
V y = new V(3,4);
}
}
3.编码创建一个手机类Phones,定义打电话方法call()。创建两个子类:苹果手机类IPhone和安卓手机类APhome,并在各自类中重写方法call(),编写程序入口main()方法中实现两种手机打电话。
public class Phones {
String pinpai;//品牌
double money;//钱
public void setpinpai(String pinpai) {
this.pinpai = pinpai;
}
public String getpinpai() {
return pinpai;
}
public void setmonet(double money) {
this.money = money;
}
public double getmoney() {
return money;
}
Phones(String pinpai,double money){
this.money = money;
this.pinpai = pinpai;
}
public void call(){
System.out.println("我正在使用"+pinpai+"牌的手机打电话,这款手机价值"+money+"元。");
}
}
public class IPhone extends Phones{
IPhone(String pinpai,double money){
super(pinpai,money);
}
public void call() {
System.out.println("我正在使用"+super.getpinpai()+"牌的手机打电话,"
+ "这款手机价值"+super.getmoney()+"元。biu~biu~biu~");
}
}
public class APhone extends Phones{
APhone(String pinpai,double money){
super(pinpai,money);
}
public void call() {
System.out.println("我正在使用"+super.getpinpai()+"牌的手机打电话,"
+ "这款手机价值"+super.getmoney()+"元。~~~~~");
}
}
public class Main {
public static void main(String[] args) {
APhone a = new APhone("安卓",3000);
a.call();
IPhone i = new IPhone("苹果",5000);
i.call();
}
}
4.阅读下面程序的程序,分析输出结果,并上机验证。
(1)程序1:
class A{
void callme( ) {
System.out.println(“Inside A’s callme()method”);
} } class B extends A{
void callme( ) {
System.out.println(“Inside B’s callme() Method”);
} } public class Dispatch{
public static void main(String args[]) {
A a=new B();
a.callme( );
} }
上面程序的输出结果:
Inside B’s callme() Method
(2)程序2:
class AA{
double x=1.1;
double method(){
return x;
}
}
class BB extends AA{
double x=2.2;
double method(){
return x;
}
}
1) 类AA和类BB是什么关系?
AA是父类,BB是子类
2) 类AA和类BB中都定义了变量x和method()方法,这种情况称为什么?
方法的重载
3) 若定义AA a=new BB(); 则a.x和a.method()的值是什么?
1.1、2.2
(3)程序3:
class AA{
public AA(){
System.out.println(“AA”);
}
public AA(inti){
this();
System.out.println(“AAAA”);
}
public static void main(String args[]){
BB b=new BB();
}
}
class BB extends AA{
public BB(){
super();
System.out.println(“BB”);
}
public BB(inti){
super(i);
System.out.println(“BBBB”);
}
}
1) 上面程序的输出结果是什么?
AA
BB
2)若将main()方法中的语句改为:B b=new B(10); 程序输出的结果是什么?
AAAA
BBBB