3、方法重载指的是在一个类中可以声明多个同名的方法,而方法中参数的个数或者数据类型不一致。
调用这些同名的方法时,JVM会根据实际参数的不同绑定到不同的方法。
六、编程题
1、参考答案
publicclassTest01{
publicstaticvoidmain(String[]args){
intsum=0;
for(inti=1;i<100;i++){
;}
if(i%2!
=0)sum+=i;
}
}
2、参考答案
publicclassTest02{
publicstaticvoidmain(Stringargs[]){
inty=function(0);
}
};
publicstaticintfunction(intx){
}inty;
if(x>0){returny;}
y=x+3;y=0;y=x*x-1;}elseif(x==0){}else{
3、参考答案
publicclassTest03{
}publicstaticvoidmain(String[]args){}int[]arr={25,24,12,76,101,96,28};for(inti=0;iarr[j+1]){//比较相邻元素}//下面的三行代码用于交换两个元素inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;
第3章面向对象(上)
一、填空题
1、封装、继承、多态
2、new
3、成员变量、局部变量
4、类、类
5、this
6、finalize()
7、静态变量
8、内部类
9、javadoc
10、private
二、判断题
1、对2、对3、错4、对5、错
三、选择题
1、B2、D3、B4、ABC5、ABCD6、ACD7、ABCD8、ABCD9、D10、D
四、程序分析题
1、程序不能编译通过,因为在类A中的成员变量secret用private修饰,所以在类Test1中无法访问。
2、程序不能编译通过,因为在静态方法method()中不能访问非静态成员变量x。
3、程序能够编译通过,运行的结果为“inner”。
五、简答题
1、构造方法是类的一个特殊成员,它会在类实例化对象时被自动调用。
而普通方法只有在使用的时候才会被调用。
在定义构造方法时要求方法名与类名相同、在方法名的前面没有返回值类型的声明、在方法中不能使用return语句返回一个值
2、单例模式可以保证在整个程序运行期间针对该类只存在一个实例对象。
六、编程题1、参考答案
classStudent{
}
publicclassTest01{
publicstaticvoidmain(String[]args){Studentstu1=newStudent();privateStringname;privatedoublegrade;publicStudent(){}publicStudent(Stringname,doublegrade){}publicStringgetName(){}publicvoidsetName(Stringname){}publicdoublegetGrade(){}publicvoidsetGrade(doublegrade){}this.grade=grade;returngrade;this.name=name;returnname;this.name=name;this.grade=grade;
}}stu1.setName("zhangsan");stu1.setGrade(99);Studentstu2=newStudent("lisi",100);
2、参考答案
classFather{
}
publicclassTest02{
publicstaticvoidmain(String[]args){}Father.Childchild=newFather().newChild();child.introFather();privateStringname="zhangjun";classChild{}publicvoidintroFather(){}"MyFather'snameis"+name);
}
第4章面向对象(下)
一、填空题
1、继承
2、方法,抽象类
3、import
4、子类、父类、基类
5、Exception
6、final
7、super
8、Object
9、try、catch
10、jar–cvf,java–jar
二、判断题
1、错2、对3、错4、对5、对
三、选择题
1、B2、C3、ABC4、ABCD5、C6、AC7、C8、D9、A10、B
四、程序分析题
1、程序编译能通过,这是因为intx=2/0;;这两条语句使用了try块,捕获了程序
因为除以0而产生的异常情况,之后程序会继续向下执行,输出“进入catch代码块”,“进入finally代码块”。
2、程序编译不通过,这是因为在程序中使用了final关键字修饰Animal类,使得Animal类不能被继
承。
shout()方法中同样使用了final关键字,使得该方法不能被重写。
3、程序编译能通过,输出结果为“动物叫!
”和“汪汪……”,因为在程序中调用shout()方法时,首先会通过super.shout()调用父类的方法说出“动物叫!
”之后再输出“汪汪……”
4、程序编译不通过,因为接口中定义的方法不能有方法体,所以定义的eat()方法是错误的。
接口中的方法必须在子类中全部实现,由于run()方法在子类中并没有重新实现,所以这也是错误的。
五、简答题
1、在继承关系中,子类的方法与父类的某一方法具有相同的方法名、返回类型和参数列表,则称子类的该方法重写(覆盖)父类的方法。
2、多态意味着一个对象有着多种形态,可以在特定的情况下,表现不同的状态,从而对应着不同的属性和方法。
简单的说,多态就是使用父类类型的变量引用子类对象,根据被引用子类对象的特性,程序会得到不同的运行效果。
3、在Java中,使用abstract关键字修饰的类称之为抽象类。
抽象类是不能被实例化的,通常需要写一个子类来继承抽象类,同时实例化子类来获得该类的对象。
抽象类通常用于表示一种抽象的概念。
接口可以说是一种特殊的抽象类,接口中只能定义常量和抽象方法。
由于接口的特殊性,在定义时需要使用interface关键字。
六、编程题1、参考答案
classStudent{
}
classUnderGraduateextendsStudent{
}
publicclassTest01{
publicstaticvoidmain(String[]args){}Studentstudent=newStudent("zhangsan",16);student.show();UnderGraduateunderGraduate=newUnderGraduate("lisi",20,"bechalor");underGraduate.show();publicStringdegree;publicUnderGraduate(Stringname,intage,Stri