}
stu_ID=id; this.name=name; this.sex=sex; this.old=old; }
void show_id(){ System.out.println(\}
void show_name(){ System.out.println(\}
void show_sex(){ System.out.println(\}
void show_old(){ System.out.println(\}
void change_old(int newyear){ old=newyear; }
public static void main(String args[]){ student Lee=new student(2007070130,\ Lee.show_id(); Lee.show_name(); Lee.show_sex(); Lee.show_old(); Lee.change_old(20); Lee.show_old(); }
8.根据下面的要求编程实现复数类Complex。
(1)复数类Complex的属性:
real代表复数的实数部分 imagin代表复数的虚数部分
Complex( ):构造函数,将实部、虚部都置为0;
Complex(double r,double i ):构造函数,创建复数对象的同时完成复数的实部、
虚部的初始化,r为实部的初值,i为虚部的初值;
getReal( ):获得复数对象的实部; getImagin( ):获得复数对象的虚部;
complexAdd(Complex Number):当前复数对象与形参复数对象相加,所得的
(2) 复数类Complex的方法:
结果也是复数值,返回给此方法的调用者;
complexMinus(Complex Number):当前复数对象与形参复数对象相减,所得
的结果也是复数值,返回给此方法的调用者;
complexMulti(Complex Number):当前复数对象与形参复数对象相乘,所得
的结果也是复数值,返回给此方法的调用者;
toString( ):把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中a
和b分别为实部和虚部的数据。
public class Complex{ private double real; private double imagin; Complex(){ real=0; imagin=0; } Complex(double r,double i){ real=r; imagin=i; } public double getReal(){ return real; } public double getImagin(){ return imagin; } public Complex complexAdd(Complex Number){ real=real+Number.real; imagin=imagin+Number.imagin; return this; } public Complex complexMinus(Complex Number){ real=real-Number.real; imagin=imagin-Number.imagin; return this; } public Complex complexMulti(Complex Number){ real=real*Number.real; imagin=imagin*Number.imagin; return this; } public void tostring(){ System.out.println(real+\ }
public static void main(String args[]){ Complex a=new Complex(); Complex b=new Complex(3,4); Complex c=new Complex(2,3); a.tostring(); b.tostring(); c.complexAdd(b); c.tostring(); }
}
[第9章]
1.设有下面两个类的定义: class Person{ long id;// 身份证号 String name://姓名 } class Student extends Person{ int score://成绩 int getScore( ){ return score;} } 则类Person和类Student的关系是 。
A、包含关系 B、继承关系 C、关联关系答案:B
2.设有如下程序:
class MyClass extends Object{ private int x; private int y;
public MyClass( ){ x=0; y=0; }
public MyClass(int x, int y){ ... ... ... }
public void show( ){
System.out.println(\ y=\ }
public void show(boolean flag){
if (flag) System.out.println(\ y=\
D、无关系
else System.out.println(\ x=\ }
protected void finalize( ) throws throwable{ super.finalize(); } }
public class MyPro{
public static void main(String args[]){ MyClass myclass=new MyClass(5,10);
System.out.println(\ y=\ } }
编译运行结果是什么? (A) x=0 y=0 (B) x=5 y=10
(C) 编译不能通过
答案:C
3.接口中可以有的语句为____;(从ABCD中多选)
一个类可以继承____父类,实现____接口;一个接口可继承____接口;(从EF中单选) 接口____继承父类,____实现其他接口;实现某个接口的类____当作该接口类型使用;(从GH中单选) (A) int x; (B) int y=0;
(C) public void aa( );
(D) public void bb( ){System.out.println(\(E) 仅一个 (F) 一个或多个 (G) 可以 (H) 不可以
答案:BC; E; F; F; H; H; G
4.解释this和super的意义和作用。
答:Java中,this用来引用当前对象,与this类似,super用来引用当前对象的父类。
5.什么是继承?继承的意义是什么?如何定义继承关系? 答:继承是一种由已有的类创建新类的机制。
通过继承可以实现代码的复用,使程序的复杂性线性地增长,而不是随规模增大呈几何级数增长。
由于父类代表了所有子类的共性,而子类既可继承其父类的共性,又可以具有本身独特的个性,在定义子类时,只要定义它本身所特有的属性与方法就可以了。
6.什么是多态?Java程序如何实现多态?有哪些实现方式?
答:多态性是指同名的不同方法在程序中共存。即为同一个方法定义几个版本,运行时根据不同情况执行不同的版本。调用者只需使用同一个方法名,系统会根据不同情况,调用相应的不同方法,从而实现不同的功能。多态性又被称为“一个名字,多个方法”。
多态性的实现有两种方式:覆盖实现多态性、重载实现多态性。
7.利用多态性编程,实现求三角形、正方形和圆形的面积。提示:抽象出一个共享父类,定义一函数为求面积的公共接口,再重新定义各形状的求面积函数。在主类中创建不同类的对象,并求得不同形状的面积。
答:利用多态性编程,实现求三角形、正方形和圆形面积。方法:抽象出一个共享父类,定义一函数为求面积的公共界面,再重新定义各形状的求面积函数。在主类中创建不同类的对象,并求得不同形状的面积。 abstract class Shape {
abstract float area(); }class Circle extends Shape {
public float r; Circle(float r) {
this.r = r; }
public float area() {
return 3.14*r*r; }
}class Rectangle extends Shape {
public float width,height; Rectangle (float w, float h) {
width = w; height = h; }
public float area() {
return width*height; } }