Java程序设计系列练习6面向对象高级基础Word下载.docx
《Java程序设计系列练习6面向对象高级基础Word下载.docx》由会员分享,可在线阅读,更多相关《Java程序设计系列练习6面向对象高级基础Word下载.docx(21页珍藏版)》请在冰点文库上搜索。
![Java程序设计系列练习6面向对象高级基础Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-4/30/6a90e056-34b6-4038-849e-e0321fc6d73d/6a90e056-34b6-4038-849e-e0321fc6d73d1.gif)
}
2.classBextendsA{voidfoo(inti){};
A第1行编译错误。
B第2行编译错误。
C代码编译成功。
5、有关类的说法正确的是(B)(知识点:
类的封装)
A.类具有封装性,所以类的数据是不能被访问的
B.类具有封装性,但可以通过类的公共接口访问类中的数据
C.声明一个类时,必须使用public修饰符
D.每个类中必须有main方法,否则程序无法运行
6、将类的访问权限设置为默认的,则该成员能被(A)(知识点:
访问权限)
A.同一包中的类访问B.其他包中的类访问
C.所有的类访问D.所有的类的子类访问
7、假设下面的程序代码都放在MyClass.java文件中,(D)程序代码能够编译通过。
Java包的概念)
A.importjava.awt.*;
packagemypackage;
calssMyClass{}
B.packagemypackage;
importjava.awt.*;
publicclassmyClass{}
C.intm;
classMyClass{}
D./*Thisisacomment*/
publicclassMyClass{}
8、假设在java源程序文件“MyClass.java”中只含有一个类,而且这个类必须能够被位于一个庞大的软件系统中的所有java类访问到,那么下面()声明有可能是符合要求的类声明。
访问权限)C
A.privateclassMyClassextendsObject
B.publicclassmyclassextendsObject
C.publicclassMyClass
D.classMyClassextendsObject
Ⅱ.程序分析题
1、分析并完善如下程序,并写出最后执行结果:
(知识点:
类的继承super关键字使用)
packagepeople;
classPerson{
Stringname="
zhangsan"
;
intage=18;
doubleheight=1.75;
publicPerson(){
System.out.println("
Person()"
);
publicPerson(Stringn){
Person(Stringn)"
name=n;
publicPerson(Stringn,inta,doubleh){
Person(Stringn,inta,doubleh)"
name=n;
age=a;
height=h;
publicvoidsayHello(){
Hello!
"
classStudent4extendsPerson{
doublescore=0;
publicStudent4(){
__this__("
aa"
10);
Student()"
publicStudent4(Stringn,doubles){
__super__(n);
score=s;
Student(Stringn,doubles)"
publicStudent4(Stringn,inta,doubleh,doubles){
__this__(n,s);
Student(Stringn,inta,doubleh,doubles)"
publicvoidstudy(){
score++;
__this/super__.sayHello();
Hello,teacher!
inti=this.age;
intj=super.age;
publicstaticvoidmain(Stringargs[]){
Student4s4=newStudent4();
__s4__.sayHello();
2、找出程序中的错误,并说明原因:
final关键字使用)
publicclassFinalVar{
finalinty=100;
finalintx1;
publicFinalVar(){
x1=1;
publicstaticvoidmain(String[]args){
finalintz=0;
publicvoidaa(){
FinalVar.add
(1);
publicstaticvoidadd(finalintx2){//final关键字修饰的x2为一个常数,不可以再将它当作变量使用
x2++;
3、找出程序中的错误,并说明原因:
finalclassFinalClass{
publicvoidadd(intx){
x++;
publicclassSubFinalDemoextendsFinalClass{//final修饰的类是不可以用来继承的
x+=2;
4、找出程序中的错误,并说明原因:
classFinalClass{
publicfinalvoidadd(intx)
{
publicclassSubFinalDemoextendsFinalClass{
publicvoidadd(intx){//fianl修饰的方法不可以被重写
Ⅲ.编程题
1、如下两个类之间存在一定的联系,请用面向对象的特性实现类及其关系:
publicclassPerson{
publicStringname;
publicintage;
publicdoubleheight;
publicPerson(Stringname,intage,doubleheight){
this.name=name;
this.age=age;
this.height=height;
publicvoidsayhello(){
System.out.println(name+age+height);
publicclassStudentextendsPerson{
publicdoublescore;
publicStudent(Stringname,intage,doubleheight,doublescore){
super("
21,183);
this.score=score;
//TODOAuto-generatedconstructorstub
publicvoidstudy(){
//TODOAuto-generatedmethodstub
Students=newStudent("
21,183,9);
2、
(1)定义一个汽车类Vehicle,要求如下:
类的继承方法的覆盖)
(a)属性包括:
汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
(c)为属性提供访问器方法。
注意:
汽车品牌一旦初始化之后不能修改。
(d)定义一个一般方法run(),用打印语句描述汽车奔跑的功能
定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车
packagecar;
publicclassVehicle{
Stringbrand;
Stringcolor;
doublespeed=0;
publicVehicle(Stringbrand,Stringcolor){
this.brand=brand;
this.color=color;
publicvoidrun(){
System.out.println("
跑的块"
publicclassVehicleTest{
publicVehicleTest(){
Vehiclev=newVehicle("
benz"
"
black"
v.run();
(2)定义一个Vehicle类的子类轿车类Car,要求如下:
(a)轿车有自己的属性载人数loader(int类型)。
(b)提供该类初始化属性的构造方法。
(c)重新定义run(),用打印语句描述轿车奔跑的功能。
(d)定义测试类Test,在其main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为
2人的轿车。
publicclassCarextendsVehicle{
intloaderl;
publicCar(Stringbrand,Stringcolor){
super(brand,color);
publicintgetLoaderl(){
returnloaderl;
publicvoidsetLoaderl(intloaderl){
this.loaderl=loaderl;
我是"
+brand+"
车"
+"
颜色为"
+color+"
限载"
+loaderl+"
人"
publicclassTest{
publicTest(){
Carc=newCar("
Honda"
"
red"
c.setLoaderl
(2);
c.run();
3、设计四个类,分别是:
抽象类及抽象方法)
(1)Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:
getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
(2)2个子类:
1)Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
2)Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
(3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。
packageshape;
publicabstractclassshqpe{
doublearea;
doubleper;
publicshqpe(){
publicabstractdoublegetArea();
publicabstractdoublegetPer();
publicabstractdoublegetshowall();
publicvoidsetColor(Stringcolor){
this.color=color;
publicclassRectangleextendsshqpe{
doublewidth;
doubleheight;
publicRectangle(){
publicdoublegetArea(){
returnwidth*height;
publicdoublegetPer(){
return2*(width*height);
publicdoublegetshowall(){
矩形的面积是"
+getArea());
矩形的周长是"
+getPer());
return0;
publicvoidsetvalue(doublewidth,doubleheight){
this.width=width;
this.height=height;
publicclasscircleextendsshqpe{
doubleradius;
publiccircle(){
returnradius*radius*3.14;
return2*radius*3.14;
圆的面积是"
圆的周长是"
publicvoidsetRadius(doubleradius){
this.radius=radius;
publicclassPolydemo{
publicPolydemo(){
Rectangler=newRectangle();
r.width=12.0;
r.height=3.0;
circlec=newcircle();
c.radius=5.0;
r.getshowall();
c.getshowall();
4、Cola公司的雇员分为以下若干类:
多态)
(1)ColaEmployee:
这是所有员工总的父类,属性:
员工的姓名,员工的生日月份。
•方法:
getSalary(intmonth)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
(2)SalariedEmployee:
ColaEmployee的子类,拿固定工资的员工。
•属性:
月薪
(3)HourlyEmployee:
ColaEmployee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。
每小时的工资、每月工作的小时数
(4)SalesEmployee:
ColaEmployee的子类,销售人员,工资由月销售额和提成率决定。
月销售额、提成率
(5)定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee数组里,并单元出数组中每个员工当月的工资。
packagecompany;
publicclassColaEmployee{
Stringname;
intbirth;
publicColaEmployee(Stringname,intbirth){
this.birth=birth;
publicintgetsalary(intsal){
returnsal;
publicclassSalaryiedemployeeextendsColaEmployee{
doublesalarymonth;
publicSalaryiedemployee(Stringname,intbirth,doublesalarymonth){
super(name,birth);
this.salarymonth=salarymonth;
publicclassSalesEmployeeextendsColaEmployee{
intxiaoshou;
doublerase;
publicSalesEmployee(Stringname,intbirth,intxiaoshou,doublerase){
this.xiaoshou=xiaoshou;
this.rase=rase;
publicclassHourlyEmployeeextendsColaEmployee{
intsalary;
inthours;
publicHourlyEmployee(Stringname,intbirth,intsalary,inthours){
this.salary=salary;
this.hours=hours;
publicclassCompany{
publicCompany(){
Salaryiedemployees=newSalaryiedemployee("
普通员工"
2,1);
SalesEmployees1=newSalesEmployee("
中级员工"
6,20000,0.5);
HourlyEmployees2=newHourlyEmployee("
高级员工"
5,100,182);
System.out.println(s.name+"
本月工资是"
+s.salarymonth*3500);
System.out.println(s1.name+"
+s1.xiaoshou*s1.rase);
System.out.println(s2.name+"
+(s2.hours-160)*s2.salary*1.5+160*s2.salary);
Ⅳ选做题
1、编写三个系别的学生类:
英语系,计算机系,文学系(要求通过继承学生