您的位置:首页 > 其它

今日学习总结

2015-08-20 00:10 267 查看
1、什么是抽象方法?什么是抽象类?

答:

如果一个方法只有定义,没有方法内容(无{}),那么该方法就是抽象方法,使用关键字abstract定义。拥有抽象方法的类,那么就是抽象类。

2、接口如何定义?接口如何实现?实现一个接口必须要做什么?

答:

接口是行为的抽象,将两个多个类相同的行为抽象到接口中。

定义接口使用关键字interface,一个类要实现接口,使用关键字implements。一个类实现一个接口,必须实现接口中定义的所有抽象方法。

3、接口中的方法修饰符是什么?接口中的变量修饰符是什么?

答:

修饰符为public abstract,变量修饰符为public static final。

4、接口和抽象类的区别是什么?

答:

1、抽象类和接口都不能产生对象(实例化);

2、接口里所有方法都是abstract的,抽象类既有方法的定义也有方法的实现;

3、接口中定义的变量必须公共的静态常量。而抽象类定义的变量是普通属性;

4、一个类只能继承于另一个类,但可以实现多个接口;

5、接口可以多继承接口,但抽象类只能单根继承。

5、什么时候使用继承?什么时候使用接口?

答:抽象属性和方法时用继承;

抽象方法时用接口。(接口中只能有抽象方法)

-----------------------------------------------------------

6、说出下面代码执行结果

class Base{}

interface ITest{}

class Man extends Base{}

class MyTest implements ITest{}

class Test{

publicstatic Man m;

publicstatic MyTest mt = new MyTest();

publicstatic void main(String argv[]){

System.out.println(m instanceof Base);

System.out.println(mt instanceof ITest);

}

}

答:

-----------------------------------------------------------

7、class Base{}

public class Child extends Base{

static byte b1=2;

static byte b2=2;

static int i = -1;

static double d = 10.1;

public static void main(String argv[]){

Child m = new Child();

Base b = new Base();

//Here

}

}

下面哪些代码,当被插入//here所指地方时,编译和运行不会出错

1) b=m;

2) m=b;

3) d =i;

4) b1 =b1+b2;

答:全都报错。

-----------------------------------------------------------

8、JAVA代码查错

a、

abstract class Name {

private String name;

public abstract boolean isStupidName(String name) {}

}

答:抽象方法未指定主题

b、 abstract class Something {

private abstract String doSomething ();

}

答:修饰符只能是可视的,如public或protected

c、public class Father{

public Father(int x){

}

}

class Child extends Father{

public Child(){

}

}

答:public 没有构造函数可以调用

d、public class Father{

public void test(){

}

}

class Child extends Father{

public void speak(){

}

}

class Test{

public static void main(String[] args){

Fatherf = new Child();

f.speak();

}

}

答:没有为类型Father 定义speak()方法

e、interface A{

intx = 0;

}

class B{

intx =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}

答:输出的x 指代不明

-----------------------------------------------------------

9、说出运行结果

1、abstract class Base{

abstract public void myfunc();

public void another(){

System.out.println("Another method");

}

}

public class Abs extends Base{

public static void main(String argv[]){

Base a = new Abs();

a. another ();

}

public void myfunc(){

System.out.println("My Func");

}

public void another (){

super.another();

myfunc();

}

}

答:Anothermethod

-----------------------------------------------------------

10、

下面哪个方法可以放在注释//here处?

public class Rid{

public void amethod(int i, String s){}

//Here

}

1)public void amethod(String s, int i){}//

2)public int amethod(int i,String s){}

3)public void amethod(int i, Stringmystring){}

4) public void Amethod(int i, String s) {}

答:(4)

-----------------------------------------------------------

11、下面哪个方法可以放在注释//here处?

class Base{

public Base(int i){}

}

public class MyOver extends Base{

public static void main(String arg[]){

MyOver m = new MyOver(10);

}

MyOver(int i){

//Here

}

}

1)MyOver m = new MyOver();

2)super();

3)Base b = new Base(10);

答: (2)

-----------------------------------------------------------

12、说出运行结果

class A{

public void speak(int a){

System.out.println("ok");

}

}

class B extends A{

public void speak(int a, int c){

System.out.println("yes");

}

public static void main(String[] args){

B b=new B();

b.speak(0);

}

}

答:ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: