实验2:类、对象、继承与接口的应用
实验内容
第2版
1.Eclipse环境下输入例4.6、例4.10(增加成员变量的get和set方法)、例4.17、例4.19,调试运行输出结果。
2.Eclipse环境下输入例5.3、例5.4、例5.9(增加成员变量的get和set方法)、例5.11、例5.12、例5.15,调试运行输出结果。
或者第3版
1.Eclipse环境下输入例4.6、例4.10(增加成员变量的get和set方法)、例4.17、例4.19,调试运行输出结果。
2.Eclipse环境下输入例5.3、例5.4、例5.9(增加成员变量的get和set方法)、例5.11、例5.12、例5.16,调试运行输出结果。
算法描述及实验步骤
(用自然语言或算法流程图的形式表达算法设计思想与算法实现步骤,注意不是贴源代码,百度搜索相关学习资料)
例4.6
新建一个Computer类,Computer创建一个求平均数的double 类型的函数getResult(double a,int …x) ,可变参数x的应用;
新建Example4_6的类,在该类中创建main函数测试Computer类中求平均数的函数,并输出平均数。
例4.10
新建一个People类,People类中包括两个int类型的变量leg和hand,一个String类型的变量name;
定义构造函数People(String s),定义int类型的函数getLeg()、void类型的函数setLeg(int leg)、int类型的函数getHand()、void类型的函数setHand(int hand);并且在构造方法中使用this关键字,使用该构造方法所创建的对象;
定义void类型的函数init()给hand和leg赋值并输出;
定义main函数,创建People类型的对象boshi测试上述函数。
例4.17
新建一个Student类,类中包括私有类型int类型的变量age,公有类型void类型的函数setAge(int age),公有类型int类型的函数getAge(),以及this关键字的应用;
新建一个Example4_17的类,新建main函数,新建Student类型的两个对象zhang和gene,分别赋值测试。
这个例子主要演示的是:当用某个类在另外一个类中创建对象后,若不希望对象直接访问自己的变量,即通过“.”运算符操作自己的成员变量,并且应将该成员变量的访问权限设置为private。
例4.19
新建一个Example4_19的类,新建main函数,创建Integer类型的对象x,y并赋值,创建Integer类型的对象m,创建int类型的变量ok并将m赋值给它,输出ok。
这个例子演示的是JDK新增功能:基本数据类型的自动装箱与拆箱,
即装箱:Integer x=new Integer(100),y=new Integer(12);
先拆箱再装箱:Integer m=new Integer(x.intValue()+y.intValue());
拆箱:int ok=m.intValue();
例5.3
创建People类,其中包括共有类型的double类型的变量x,以及它的get和set函数;
创建People的子类Student,其中包括int类型的变量x,和它的get函数,此时子类创建了与父类同名的变量就把父类double类型的x隐藏,但是子类对象可以调用从父类继承的方法操作隐藏的double型成员变量;
创建main函数测试上述类,子类对象调用继承的方法操作隐藏的double型变量x,并实现输出对象隐藏的x的值。
例5.4
新建一个类A,其中包括double类型的求和函数f(float x,float y)和公有类型的int类型的求和函数g(int x,int y);
新建一个A的子类B,其中包括double类型的求乘积函数f(float x,float y);
定义一个Example5_4类,新建main函数调用上述函数,b分别调用重写的方法和继承的方法,并输出各自得到的结果,子类中与父类重名的函数会覆盖父类的函数。
例5.9
新建一个类Anthropoid,其中包括double类型的常量m,void类型的函数crySpeak(String s),新建m的get和set函数;
新建类Anthropoid的子类People,其中包括char类型的变量m,int类型的常量n,void类型的函数computer(int a,int b),void类型的函数crySpeak(String s),新建m、n的get和set函数;
新建类Example5_11,测试上述类,Anthropoid类声明的对象monkey是People类创建的对象people的上转型对象,然后monkey调用重写的crySpeak方法,然后输出操作隐藏的m和操作子类的m,并把上转型对象强制转化为子类的对象zhang,最后输出操作子类声明的成员的变量m。
例5.11
新建抽象类A,其中包括抽象int类型的函数sum(int x,int y),int 类型的函数sub(int x,int y);
新建类A的子类B,其中包括重写父类的函数sum(int x,int y)
[如果一个非抽象类是某个抽象类的子类,那么它必须重写父类的抽象方法,给出方法体];
新建类Example5_11,测试上述类,分别调用重写的方法和调用继承的方法,最后输出sum和sub。
例5.12
新建抽象类“机动车”,其中包括三个抽象的void类型的函数 启动()、加速()、刹车();
新建“机动车”的子类“手动挡轿车”和“自动挡汽车”,分别对父类三个抽象函数进行实现并且输出(机动车类的非抽象子类必须给出启动、加速和刹车的细节);
新建类Example5_12,创建主函数对上述类创建对象测试。
例5.15
先是关键字interface进行两个接口“收费”和“调节温度”的声明;
接下来是接口的使用:即类“公共汽车”实现收费接口,类“出租车”实现收费、调节温度接口,类“电影院”实现收费、调节温度接口;
然后是新建类Example5_15,声明接口变量,接口变量中存放对象的引用,根据相应需要实现相应的接口。
代码
例4.6
package chapter4;
public class Computer {
public double getResult(double a,int ... x) { //x是可变参数的参数代表
double result =0;
int sum=0;
for(int i=0;i<x.length;i++) {
sum=sum+x[i];
}
result=a*sum;
return result;
}
}
package chapter4;
public class Example4_6 {
public static void main(String[] args) {
Computer computer=new Computer();
double result=computer.getResult(1.0/3,10,20,30);//参数代表x代表了三个参数
System.out..println("10,20,30的平均数:"+result);
result=computer.getResult(1.0/6, 66,12,5,89,2,51);
System.out..println("66,12,5,89,2,51的平均数:"+result);
}
}
例4.10
//成员变量的set()和get()方法
public class People {
int leg,hand;
String name;
People(String s){
name=s;
this.init();
}
public void setLeg(int leg) {
this.leg=leg;
}
public void sethand(int hand) {
this.hand=hand;
}
public void setName(String name) {
this.name=name;
}
public int getleg() {
return leg;
}
public int gethand() {
return hand;
}
public String getName() {
return name;
}
void init() {
leg=2;
hand=2;
System.out.println(name+"有"+hand+"只手"+leg+"条腿");
}
public static void main(String[] args) {
People boshi=new People("布什");
}
}
例4.17
Student.java
package chapter4;
public class Student {
private int age;
public void setAge(int age) {
if(age>=7&&age<=28) {
this.age=age;
}
}
public int getAge() {
return age;
}
}
Example4_17.java
package chapter4;
public class Example4_17 {
public static void main(String[] args) {
Student zhang=new Student();
Student geng=new Student();
zhang.setAge(23);
System.out..println("zhang的年龄:"+zhang.getAge());
geng.setAge(25);
System.out..println("geng的年龄:"+geng.getAge());
}
}
例4.19
package chapter4;
public class Example4_19 {
public static void main(String[] args) {
Integer x=100,y=12;
Integer m=x+y;
int ok=m;
System.out..println(ok);
}
}
例5.3
package 课本例题chapter5;
class People{
public double x;
public void setX(double x) {
this.x=x;
}
public double getDoubleX() {
return x;
}
}
class Student extends People{
int x;
public int getX() {
return x;
}
}
public class Example5_3 {
public static void main(String[] args) {
Student stu=new Student();
stu.x=98;
System.out..println("对象stu的x的值是:"+stu.getX());
stu.setX(98.98);
double m=stu.getDoubleX();
System.out..println("对象stu 隐藏的x的值是:"+m);
}
}
例5.4
package 课本例题chapter5;
class A{
double f(float x,float y) {
return x+y;
}
public int g(int x,int y) {
return x+y;
}
}
class B extends A{
double f(float x,float y) {
return x*y;
}
}
public class Example5_4 {
public static void main(String[] args) {
B b=new B();
double result=b.f(5, 6); //b调用重写的方法
System.out..println("调用重写方法得到的结果:"+result);
int m=b.g(3, 5); //b调用继承的方法
System.out..println("调用继承方法得到的结果:"+m);
}
}
例5.9
Anthropoid.java
public class Anthropoid {
double m=12.58;
void crySpeak(String s) {
System.out.println(s);
}
public void setM(double m) {
this.m=m;
}
public double getM() {
return m;
}
}
People.java
public class People extends Anthropoid {
char m='A';
int n=60;
void computer(int a,int b) {
int c=a+b;
System.out.println(a+"加"+b+"等于"+c);
}
void crySpeak(String s) {
System.out.println(m+"**"+s+"**"+m);
}
public void setm(char m) {
this.m=m;
}
public char getm() {
return m;
}
public void setn(int n) {
this.n=n;
}
public int getn() {
return n;
}
}
Example5_9.java
public class Example5_9 {
public static void main(String[] args) {
People people=new People();
Anthropoid monkey=people; //monkey是people对象的上转型对象 monkey.crySpeak("I Love this game"); //等同于people调用重写的crySpeak方法
System.out.println(monkey.m); //操作隐藏的m,不等同于people.m
System.out.println(people.m); //操作子类的m
People zhang=(People)monkey; //把上转型对象强制转化为子类的对象
zhang.computer(55, 33); //zhang是子类的对象
zhang.m='T'; //操作子类声明的成员的变量m
System.out.println(zhang.m);
}
}
例5.11
package chapter5;
abstract class A {
abstract int sum(int x,int y);
int sub(int x,int y) {
return x-y;
}
}
class B extends A{
int sum(int x,int y) {
return x+y;
}
}
public class Exa5_11 {
public static void main(String[] args) {
B b=new B();
int sum=b.sum(30,20);
int sub=b.sub(30, 20);
System.out..println("sum="+sum);
System.out..println("sub="+sub);
}
}
例5.12
abstract class 机动车{
abstract void 启动();
abstract void 加速();
abstract void 刹车();
}
class 手动挡轿车 extends 机动车{
void 启动() {
System.out.println("踏下离合器,换到一档");
System.out.println("然后慢慢抬起离合器");
}
void 加速() {
System.out.println("踩油门");
}
void 刹车() {
System.out.println("踏下离合器,踏下刹车板");
System.out.println("然后将挡位换到一档");
}
}
class 自动挡轿车 extends 机动车{
void 启动() {
System.out.println("使用前进挡");
System.out.println("然后轻踩油门");
}
void 加速() {
System.out.println("踩油门");
}
void 刹车() {
System.out.println("踏下刹车板");
}
}
public class Example5_12 {
public static void main(String[] args) {
机动车 car=new 手动挡轿车();
System.out.println("手动挡轿车的操作:");
car.启动();
car.加速();
car.刹车();
car=new 自动挡轿车();
System.out.println("自动挡轿车的操作:");
car.启动();
car.加速();
car.刹车();
}
}
例5.16
interface 收费{
public void 收取费用();
}
interface 调节温度{
public void controlTemperature();
}
class 公共汽车 implements 收费{
public void 收取费用() {
System.out.println("公共汽车:1元/张,不计算公里数");
}
}
class 出租车 implements 收费,调节温度{
public void 收取费用() {
System.out.println("出租车:3.20元/公里,起价3公里");
}
public void controlTemperature() {
System.out.println("安装了Hair空调");
}
}
class 电影院 implements 收费,调节温度{
public void 收取费用() {
System.out.println("电影院:门票,10元/张");
}
public void controlTemperature() {
System.out.println("安装了中央空调");
}
}
public class Example5_15 {
public static void main(String[] args) {
公共汽车 七路=new 公共汽车();
出租车 天宇=new 出租车();
电影院 红星=new 电影院();
七路.收取费用();
天宇.收取费用();
红星.收取费用();
天宇.controlTemperature();
红星.controlTemperature();
}
}