1…如下代码,编译能通过吗,为什么?
class Foo {
int value;
public void f() {
}
}
class Goo extends Foo {
int num;
public Goo(int value, int num) {
this.value = value;
this.num = num;
}
public void g() {
}
}
public class TestFoo {
public static void main(String[] args) {
Foo obj = new Goo(100, 3);
obj.value = 200;//OK
obj.f();//OK
obj.num = 5;//error 父类引用不能访问子类特有属性和方法
obj.g();//error
}
}
2.以下代码输出结果
class One {
public One() {
super();
System.out.print(1);
}
}
class Two extends One {
public Two() {
super();
System.out.print(2);
}
}
class Three extends Two {
public Three() {
super();
System.out.print(3);
}
}
public class Numbers {
public static void main(String[] argv) {
new Three();
}
}
3.如下代码,以下那些选项可以通过编译
class ClassA {}
class ClassB extends ClassA {}
class ClassC extends ClassA {}
public class Test{
public static void main(String[] args) {
ClassA p0 = new ClassA();
ClassB p1 = new ClassB();
ClassC p2 = new ClassC();
ClassA p3 = new ClassB();//多态
ClassA p4 = new ClassC();//多态
<插入代码>
}
}
A.p0 = p1;//ok 多态
B.p1 =p2; //error 两个子类,没有继承关系
C.p2 = p4;//error 不能将大类型赋值给小类型,除了强转
D.p2 = (ClassC)p1; //error 没有继承关系
4.在写代码时,需要约定子类必须实现方法时,如何选择抽象类或者接口?
抽象方法,接口,抽象类
抽象类:
不能创建对象,定义普通方法,需要有普通
方法和抽象方法时,选择抽象类
接口:不能创建对象,规范,可以多实现
5.JDK8中对接口中定义内容做了修改
可以如下定义
interface 接口名
{
常量
抽象方法
静态方法
默认方法
}
默认方法的作用,静态方法的限制?
默认方法作用,避免子类实现所有抽象方法
静态方法只能接口名调用
6.请看下面代码,写出输出结果
class A {
public void start() {
System.out.println("A Start");
}
}
public class B extends A {
public void start() {
System.out.println("B Start");
}
public static void main(String[] args) {
((A) new B()).start();
// A a = new B();
// a.start();
}
}
7.以下接口能编译成功吗
public interface A {
public void doSomething(String thing);
}
interface B {
}
interface C extends A, B {
}
interface D {
public void doIt(String thing);
}
class AImpl implements C ,D{
public void doSomething(String msg) {}
public void doIt(String thing) {}
}
8.运行以下代码
public class Animal {
public String noise() {
return "Animal";
}
public static void main(String[] args) {
Cat cat = null;
Animal animal = new Dog();
if (animal instanceof Cat) {
//instanceof是一个运算符,
//判断对象是否是某个类型
cat = (Cat) animal;
System.out.println(cat.noise());
} else {
System.out.println("animal is not Cat's instance");
}
}
}
class Dog extends Animal {
public String noise() {
return "Dog";
}
}
class Cat extends Animal {
public String noise() {
return "Cat";
}
}
9.以下代码输出结果
public static void main(String[] args){
String str1 = "NASDAQ";
String str2 = "NASDAQ";
System.out.print(str1 == str2);
//true
//原因:虚拟机针对字符串的字面量做了优化
//常量池中找,如果有则返回
//如果没有,创建一个对象,将地址放到常量池
System.out.print(",");
String str3 = new String("NASDAQ");
System.out.println(str1 == str3);
}
10.以下代码输出正确的是
public class Test(){
public static void main(String[] args){
String s1 = "runoob";
String s2 = "runoob";
System.out.println("s1==s2 is:"+ s1 == s2)
}
}
A true B s1==s2 is:false C s1==s2 is:true D false
11.以下代码
Point{
private int x;
private int y;
//get,set
}
重写equals()代码使坐标x,y相同时,两个对象相同,
并输出对象字符串描述(重写toString())
public class Point{
int x;
int y;
//重写equlas()
public boolean equals(Object o){
//如果o==this
if(o == this){
return true;
}
// o==null
if(o == null){
return false;
}
// this和o的类对象是不是同一个
if(this.getClass() != o.getClass()){
return false;
}
//如果是同一个,那么就要进行相应的逻辑比较
Point p = (Point)o;
if(this.x == p.x && this.y==p.y){
return true;
}
return false;
}
}