2、
abstract class Person{
private String name;
public Person(String n){
name = n;}
public ____abstract________ String getDescription();
public _____String getName________ (){
return name;
}
}
class Student __extends__ _________ Person{
private String major;
public Student(String n, String m){
super(n);
major = m; }
public String _____getDescription(){
return "学生专业是:" + major;
}
}
public class TestPerson{
public static void main(____String[] args________){
Person p = new Student("王甜甜", "计算机科学");
System.out.println(p.getName() + ", "+ p.getDescription());
}
}
3、interface Area 声明一个接口,不同的类实现该接口
4、interface Shape{
double PI;//这里没有初始化double PI = 3.14;
public double area( );
public double perimeter( );
}
class Cycle extends Shape{ //是实现接口 用关键字 implements
private double r;
public Cycle(double r){
this.r=r;
}
double area( ){//接口定义的类型为public 这里也必须是,
//该类中还必须实现public double perimeter( );
System.out.println(PI*r*r);//缺少返回值
}
}
public class Test{
public static void main(String args[]){
Cycle c=new Cycle(1.5);
System.out.println("面积为:"+c.area());
}
}
5、阅读下面的程序,修改程序中错误的地方(提示:共三处错误)
1. class Person{
2. String name;
3. int age;
4. String sex;
5. public Person(String name,int age,String sex){
6. this.name=name;
7. this.age=age;
8. this.sex=sex;
9. }
10. public void toString(){
11. return "name:"+name+" age:"+age+" sex:"+sex; //void 类型返回了字符串,把void 改成 String
12. }
13. }
14. public class TestPerson{
15. public static void main(String args[]){
16. Person p=new Person();//缺少参数
17. p.name="张三";
18. p.age=20;
19. p.sex="男";
20. System.out.println(Person.tostring());//Person改成p
21. }
22. }
2、仔细阅读下面的程序代码,请将划线上的语句补充完整。
abstract class Person{
private String name;
public Person(String n){
name = n;}
public ______abstract ______ String getDescription();
public __String___getName________ (){
return name;
}
}
class Student ___extends_ _________ Person{
private String major;
public Student(String n, String m){
super(n);
major = m; }
public String _______getMajor______(){
return "学生专业是:" + major;
}
}
public class TestPerson{
public static void main(____String[] args________){
Person p = new Student("王甜甜", "计算机科学");
System.out.println(p.getName() + ", "+ p.getDescription());
}
}
3、阅读下面的程序代码,根据注释要求补充、完成代码(划线是需要补充的地方),最后简要说明该程序的功能。
1. ________Public interface Area___________ { //声明一个接口,接口名为Area
2. public double CalsulateArea();
3. }
4. class MyCircle implements Area{
5. double r;
6. public MyCircle(double r){
7. this.r=r;
8. }
9. public double CalsulateArea(){
10. return Math.PI*r*r;
11. }
12. }
13. class MyRectangle implements Area{
14. double width,height;
15. public MyRectangle(double w,double h){
16. width=w;
17. height=h;
18. }
19. }
20. public double CalsulateArea(){
21. return width*height;
22. }
23. }
24. class TestArea{
25. public static void main(String []args){
26. //创建MyCircle的对象,对象名为c
27. System.out.println("The area of the Circle is "+c.CalsulateArea());
28. //创建MyRectangle的对象,对象名为r
29. System.out.println("The area of the Rectangle is "+r.CalsulateArea());
30.
31. }
32. }
4、阅读下面的程序,修改程序中编译错误的地方(提示:共五处错误)
interface Shape{
public double area( );
public double perimeter( );
}
class Cycle implements Shape{
private double r;
public Cycle(double r){
this.r=r;
}
public double area( ){
return PI*r*r;
}
public double perimeter( ){
return 2*PI*r;
}
}
public class Test{
public static void main(String args[]){
Cycle c=new Cycle(1.5);
System.out.println("面积为:"+c.area());
}
}
5、阅读下面的程序,修改程序中错误的地方(提示:共三处错误)
1. class Person{
2. String name;
3. int age;
4. String sex;
5. public Person(String name,int age,String sex){
6. this.name=name;
7. this.age=age;
8. this.sex=sex;
9. }
10. public String toString(){
11. return "name:"+name+" age:"+age+" sex:"+sex;
12. }
13. }
14. public class TestPerson{
15. public static void main(String args[]){
16. Person p=new Person("张三",20,"男");
20. System.out.println(p.tostring());
21. }
22. }
72 构造方法不需要void关键字 void X()可以试一个类方法
88 >> 89右移位除以2的一次方 89>>4 = 89/2*2*2*2
123,静态方法只能访问静态变量;
134,Java语言:单继承多实现;
143,object是所有类的父类
193、开始时x=3 注意while没有大括号,其范围是它的下一条语句,即x+=2;
执行第一次是条件是x=3 <9, 第二次是x=5<0 第三次是x=7<9,
注意是先判断条件
三次执行完后才执行x++这句。