几个面试题

package com.test;


public class Test {
    public static void main(String[] args)  {
        new Circle();
    }
}
 
class Draw {
     
    public Draw(String type) {
        System.out.println(type+" draw constructor");
    }
}
 
class Shape {
    private Draw draw = new Draw("shape");
     
    public Shape(){
        System.out.println("shape constructor");
    }
}
 
class Circle extends Shape {
    private Draw draw = new Draw("circle");
    public Circle() {
        System.out.println("circle constructor");
    }

}


结果:shape draw constructor
shape constructor
circle draw constructor

circle constructor


第二题:

package com.test;


public class Test {
    public static void main(String[] args)  {
        Shape shape = new Circle();
        
       // shape constructor
//       / circle constructor
        
        System.out.println(shape.name);//cycle
        shape.printType();//this is circle
        shape.printName();//shap
    }
}
 
class Shape {
    public String name = "shape";
     
    public Shape(){
        System.out.println("shape constructor");
    }
     
    public void printType() {
        System.out.println("this is shape");
    }
     
    public static void printName() {
        System.out.println("shape");
    }
}
 
class Circle extends Shape {
    public String name = "circle";
     
    public Circle() {
        System.out.println("circle constructor");
    }
     
    public void printType() {
        System.out.println("this is circle");
    }
     
    public static void printName() {
        System.out.println("circle");
    }

}


结果出乎我意料:


shape constructor
circle constructor
shape
this is circle

shape

说明:覆盖只针对非静态方法(终态方法不能被继承,所以就存在覆盖一说了),而隐藏是针对成员变量和静态方法的。

4:java String 引用类型

在Java中,数组和String字符串都不是基本数据类型,它们被当作类来处理,是引用数据类型。
引用类型(reference type)指向一个对象,不是原始值,指向对象的变量是引用变量
在java里面除去基本数据类型的其它类型都是引用数据类型,自己定义的class类都是引用类型,可以像基本类型一样使用。
 
 

5:冒泡

/**
 * 冒泡排序的第一种实现, 没有任何优化
 * @param a
 * @param n
 */
public static void bubbleSort1(int [] a, int n){
    int i, j;

    for(i=0; i<n; i++){//表示n次排序过程。
        for(j=1; j<n-i; j++){
            if(a[j-1] > a[j]){//前面的数字大于后面的数字就交换
                //交换a[j-1]和a[j]
                int temp;
                temp = a[j-1];
                a[j-1] = a[j];
                a[j]=temp;
            }
        }
    }
}// end


猜你喜欢

转载自blog.csdn.net/seareal1/article/details/80340511