实验指导0418.docx

上传人:b****5 文档编号:29882219 上传时间:2023-08-03 格式:DOCX 页数:46 大小:27.73KB
下载 相关 举报
实验指导0418.docx_第1页
第1页 / 共46页
实验指导0418.docx_第2页
第2页 / 共46页
实验指导0418.docx_第3页
第3页 / 共46页
实验指导0418.docx_第4页
第4页 / 共46页
实验指导0418.docx_第5页
第5页 / 共46页
点击查看更多>>
下载资源
资源描述

实验指导0418.docx

《实验指导0418.docx》由会员分享,可在线阅读,更多相关《实验指导0418.docx(46页珍藏版)》请在冰豆网上搜索。

实验指导0418.docx

实验指导0418

实验10类

班级___________学号__________________________

(一)实验目的

(1)掌握面向对象的思想;

(2)掌握类的构成及实现方法;

(3)掌握类的使用方法。

(二)实验容和步骤

1、阅读程序,首先根据自己的判断写出程序的执行结果,然后运行程序,依据程序运行结果,对比分析给出程序执行流程的解释。

(1)第一题

classFu{

publicintnum=10;

publicFu(){

System.out.println("fu");

}

}

classZiextendsFu{

publicintnum=20;

publicZi(){

System.out.println("zi");//fuzi302010

}

publicvoidshow(){

intnum=30;

System.out.println(num);

System.out.println(this.num);

System.out.println(super.num);

}

}

classTest{

publicstaticvoidmain(String[]args){

Ziz=newZi();

z.show();

}

}

(2)第二题目

classStudent{

static{

System.out.println("Student静态代码块");

}

{

System.out.println("Student构造代码块");

}

publicStudent(){

System.out.println("Student构造方法");

}

}

classStudentDemo{

static{

System.out.println("外包14,我们的大家庭");

}

publicstaticvoidmain(String[]args){

System.out.println("我是main方法");

Students1=newStudent();

Students2=newStudent();

}

}

外包14,我们的大家庭

我是main方法

Student静态代码块

Student构造代码块

Student构造方法

Student构造代码块

Student构造方法

Static只执行一次

(3)第三题

classFu{

static{

System.out.println("静态代码块Fu");

}

{

System.out.println("构造代码块Fu");

}

publicFu(){

System.out.println("构造方法Fu");

}

}

classZiextendsFu{

static{

System.out.println("静态代码块Zi");

}

{

System.out.println("构造代码块Zi");

}

publicZi(){

System.out.println("构造方法Zi");

}

}

classFuZiTest{

publicstaticvoidmain(String[]args){

Ziz=newZi();

}

}

静态代码块Fu

静态代码块Zi

构造代码块Fu

构造方法Fu

构造代码块Zi

构造方法Zi

优先执行static

2、编程题目

设手机具有下面信息:

属性:

品牌,价格,颜色

行为:

打,发短信,玩游戏

根据手机具有的信息,写一个标准的手机类,并对其进行了测试。

实验步骤:

classphone{

Stringbrand;

intprize;

Stringcolour;

publicvoidplay(){

System.out.println("playphone");

}

publicvoidplaymessage(){

System.out.println("message");

}

publicvoidplaygame(){

System.out.println("game");

}

}

classTest{

publicstaticvoidmain(String[]args){

phonep=newphone();

p.brand="iphone";

p.colour="red";

p.prize=5000;

p.playgame();

p.play();

p.playmessage();

System.out.println("brand:

"+p.brand);

}

}

 

•方法重写和方法重载的区别?

方法重载能改变返回值类型吗?

ØOverload

ØOverride

•this关键字和super关键字分别代表什么?

以及他们各自的使用场景和作用。

2、标准的学生类及测试

•定义一个类Demo,其中定义一个求两个数据和的方法,定义一个测试了Test,进行测试。

•定义一个长方形类,定义求周长和面积的方法,然后定义一个测试了Test2,进行测试。

•定义一个员工类,自己分析出几个成员,然后给出成员变量,构造方法,getXxx()/setXxx()方法,以及一个显示所有成员信息的方法。

并测试。

•定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。

(需要键盘输入参数)

3:

面试题

classX{

Yb=newY();

X(){

System.out.print("X");

}

}

classY{

Y(){

System.out.print("Y");

}

}

publicclassZextendsX{

Yy=newY();

Z(){

System.out.print("Z");

}

publicstaticvoidmain(String[]args){

newZ();

}

}

看程序写结果:

C:

子父类的初始化(分层初始化)

先进行父类初始化,然后进行子类初始化。

结果:

YXYZ

问题:

虽然子类中构造方法默认有一个super()

初始化的时候,不是按照那个顺序进行的。

而是按照分层初始化进行的。

它仅仅表示要先初始化父类数据,再初始化子类数据。

*/

classX{

Yb=newY();

X(){

System.out.print("X");

}

}

classY{

Y(){

System.out.print("Y");

}

}

publicclassZextendsX{

Yy=newY();

Z(){

//super

System.out.print("Z");

}

publicstaticvoidmain(String[]args){

newZ();

}

}

 

实验11多态、接口、包与部类

(1)

班级___________学号__________________________

(一)实验目的

1、掌握多态的设计思想及实现方法;

2、掌握接口的特点、构成及实现方法;

3、掌握包的作用及使用方法。

4、掌握部类的特点及实现方法。

(二)实验容和步骤

1、阅读程序,先判断有没有问题,如果没有,根据自己的判断写出程序的执行结果,然后运行程序,依据程序运行结果,对比分析给出程序执行流程的解释。

(1)第1题

classFu

{

publicvoidshow()

{

System.out.println("fushow");

}

}

classZiextendsFu

{

publicvoidshow()

{

System.out.println("zishow");

}

publicvoidmethod()

{

System.out.println("zimethod");

}

}

classTest

{

publicstaticvoidmain(String[]args)

{

Fuf=newZi();

Ziz=(Zi)f

//z.method();

//父类不能继承子类的方法

f.method();

}

}

(2)第2题

classA

{

publicvoidshow()

{

show2();

}

publicvoidshow2()

{

System.out.println("我");

}

}

classBextendsA

{

publicvoidshow2()

{

System.out.println("爱");

}

}

classCextendsB

{

publicvoidshow()

{

super.show();

}

publicvoidshow2()

{

System.out.println("你");

}

}

 

publicclassTest

{

publicstaticvoidmain(String[]args)

{

Aa=newB();

a.show();

 

Bb=newC();

b.show();

}

}

//重写爱你

继承的时候:

子类中有和父类中一样的方法,叫重写。

子类中没有父亲中出现过的方法,方法就被继承过来了。

(3)第3题

判断下面的赋值哪些是正确的

classAnimal{}

classDogextendsAnimal{}

classCatextendsAnimal{}

以及

Animalp0=newAnimal();

Dogp1=newDog();

Catp2=newCat();

Animalp3=newDog();

Animalp4=newCat();

p0=p1;

p1=p2;//error

p1=(Dog)p3;

p2=(Cat)p4;

2、假如我们在开发一个系统时需要对员工类进行设计,员工包含3个属性:

、工号以及工资。

经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。

请使用继承的思想设计出员工类和经理类。

要求类中提供必要的方法进行属性访问。

此外,经理在处理外包项目时需要与外籍人员进行沟通交流,要求有能用英语与其进行交流的能力。

classstaff{

privateStringname;

privateintnum;

privateintsalary;

publicstaff(){}

publicstaff(Stringname,intnum,intsalary){

this.name=name;

this.num=num;

this.salary=salary;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicintgetNum(){

returnnum;

}

publicvoidsetNum(intnum){

this.num=num;

}

publicintgetSalary(){

returnsalary;

}

publicvoidsetSalary(intsalary){

this.salary=salary;

}

}

classmanagerextendsstaff{

privateintbonus;

publicmanager(){}

publicmanager(Stringname,intnum,intsalary,intbonus){

super(name,num,salary);

this.bonus=bonus;

}

publicvoidexchange(){

System.out.println("speakingEnlish");

}

publicintgetBonus(){

returnbonus;

}

publicvoidsetBonus(intbonus){

this.bonus=bonus;

}

}

classTest{

publicstaticvoidmain(String[]args){

staffone=newstaff("one",1,1000);

managertwo=newmanager("two",2,2000,1000);

System.out.println("name:

"+one.getName()+"----num:

"+one.getNum()+"----salary:

"+one.getSalary());

System.out.println("name:

"+two.getName()+"----num:

"+two.getNum()+"----salary:

"+two.getSalary()+"----bonus:

"+two.getBonus());

}

}

 

实验12多态、接口、包与部类

(2)

班级___________学号__________________________

(一)实验目的

1、掌握多态的设计思想及实现方法;

2、掌握接口的特点、构成及实现方法;

3、掌握包的作用及使用方法。

4、掌握部类的特点及实现方法。

5、熟悉链式编程方法。

(二)实验容和步骤

1、阅读程序,先判断有没有问题,如果没有,根据自己的判断写出程序的执行结果,然后运行程序,依据程序运行结果,对比分析给出程序执行所得结果的解释。

abstractclassAbsArgRef{

publicabstractvoidshow();

}

classAbsArgRefDemo{

publicvoidmethod(Bp){

p.show();

}

}

classBextendsAbsArgRef{

publicvoidshow(){

System.out.println("这里是抽象类引用测试");

}

}

interfaceIntArgRef{

publicabstractvoidshow();

}

classIntArgRefDemo{

publicCgetC(){

returnnewC();

}

}

classCimplementsIntArgRef{

publicvoidshow(){

System.out.println("这里是接口引用测试");

}

}

classReferenceTest{

publicstaticvoidmain(String[]args){

IntArgRefDemoiard=newIntArgRefDemo();

IntArgRefiar=iard.getC();

iar.show();

newAbsArgRefDemo().method(newB());

newIntArgRefDemo().getC().show();

AbsArgRefDemoaard=newAbsArgRefDemo();

Bb=newB();

aard.method(b);

}

}

2、编程题

现需要开发一个战斗类游戏,游戏中涉及三个角色,每个角色均有一个ID号、血量两个属性,每个角色都有同一个移动(move)方法,此外每个角色都有一项必杀技,分别是降龙掌(surDragonPalm)、六脉剑(sixPulseSowrd)和一阳指(oneSunFinger)。

请设计一个类,提供对角色ID号、血量的设置和查询,提供角色移动方法和必杀技方法,方法的输出可以使用一个打印相应方法被调用的语句来模拟,并对设计的类进行测试。

要求设计的类中必须使用到面向对象设计中的抽象类和接口。

实验步骤:

1、。

请在此处对第1题进行解释

2、。

请在此处填写第2题代码

 

实验13包与部类(3)

班级___________学号__________________________

(一)实验目的

1、掌握JAVA包的作用;

2、掌握带包与不带包编译与运行的方法;

3、掌握JAVA中权限修饰符的作用围;

4、掌握部类的特点及实现方法。

5、熟悉匿名部类的使用方法。

(二)实验容和步骤

1、阅读下面程序,完成题目中的要求。

(1)补齐程序(注意:

部类和外部类没有继承关系)

classOuter{

publicintnum=10;

classInner{

publicintnum=20;

publicvoidshow(){

intnum=30;

System.out.println(?

);

System.out.println(?

);

System.out.println(?

);

}

}

}

请在?

号处理填写恰当语句,并编写测试类对其进行测试,要求程序输出结果为:

30,20,10

(2)按照要求,补齐代码,要求在控制台输出”HelloWorld”。

interfaceInter{voidshow();}

classOuter{//补齐代码}

classOuterDemo{

publicstaticvoidmain(String[]args){

Outer.method().show();

}

}

 

2、编程题

现需要开发一个战斗类游戏,游戏中涉及三个角色,每个角色均有一个ID号、血量两个属性,每个角色都有同一个移动(move)方法,此外每个角色都有一项必杀技,分别是降龙掌(surDragonPalm)、六脉剑(sixPulseSowrd)和一阳指(oneSunFinger)。

请设计一个类,提供对角色ID号、血量的设置和查询,提供角色移动方法和必杀技方法,方法的输出可以使用一个打印相应方法被调用的语句来模拟,并对设计的类进行测试。

要求使用抽象类来完成角色类的设计,使用接口来完成必杀技的设计,并且要求角色类及其子类放在com.experiment包中,接口放在包cn.gdpu包中。

实验步骤:

1、。

请在此处对第1题进行解释

2、。

请在此处填写第2题代码

 

注意,从此次实验开始,需要提交源程序,请将源程序和实验报告一起打包,并按要求给压缩包命名后发送到指定

 

1

(1)补齐程序题参考答案。

classOuter{

publicintnum=10;

classInner{

publicintnum=20;

publicvoidshow(){

intnum=30;

System.out.println(num);

System.out.println(this.num);

System.out.println(Outer.this.num);

}

}

}

classOuterDemo{

publicstaticvoidmain(String[]args){

Outer.Inneroi=newOuter().newInner();

oi.show();

}

}

 

实验14部类与Eclipse(3)

班级___________学号__________________________

(一)实验目的

1、掌握部类的特点及实现方法。

2、熟悉匿名部类的使用方法;

3、掌握Eclipse开发Java程序的方法与技巧。

(二)实验容和步骤

1、阅读下面程序,完成题目中的要求。

(1)补齐程序(注意:

部类和外部类没有继承关系)

classOuter{

publicintnum=10;

classInner{

publicintnum=20;

publicvoidshow(){

intnum=30;

System.out.println(?

);

System.out.println(?

);

System.out.println(?

);

}

}

}

请在?

号处理填写恰当语句,并编写测试类对其进行测试,要求程序输出结果为:

30,20,10

(2)按照要求,补齐代码,要求在控制台输出”HelloWorld”。

interfaceInter{voidshow();}

classOuter{//补齐代码}

classOuterDemo{

publicstaticvoidmain(String[]args){

Outer.method().show();

}

}

 

2、编程题

现需要开发一个战斗类游戏,游戏中涉及三个角色,每个角色均有一个ID号、血量两个属性,每个角色都有同一个移动(move)方法,此外每个角色都有一项必杀技,分别是降龙掌(surDragonPalm)、六脉剑(sixPulseSowrd)和一阳指(oneSunFinger)。

请设计一个类,提供对角色ID号、血量的设置和查询,提供角色移动方法和必杀技方法,方法的输出可以使用

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 动态背景

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1