Java面试题(持续更新)

多态

public class Test1{
	public static void main(String[] args){
		//运行结果为?
		Base base =new Sub();
		base.add(1,2,3);
	}
}
class Base{
	public void add(int a,int... arr){
		System.out.println("base")
	}
}
class Sub extends Base{
	public void add(int a, int[] arr){
		System.out.println("sub1");
	}
}
/*运行结果为sub1,由于Sub中的add方法重写了父类中的add所以结果为sub1*/
public class Test1{
	public static void main(String[] args){
		//运行结果为?
		Base base =new Sub();
		base.add(1,2,3);
	}
}
class Base{
	public void add(int a,int... arr){
		System.out.println("base")
	}
}
class Sub extends Base{
	public void add(int a, int[] arr){
		System.out.println("sub1");
	}
	public void add(int a, int b, int c){
		System.out.println("sub2");
	}
}
//运行结果为sub2,由于多态性,所以base只调用重写了父类中方法的方法。而add(int a, int b, int c)并没有重写父类中的方法。

简易的重写equals方法

public User{
	String name;
	int age
	public boolean equals(Object o){
		//比较两个对象的地址值
		if(o == this){
			return true;
		}
		//判断传入对象是否与User存在多态
		if(o instanceof User){
			User u = (User)o;
			return this.age == u.age;
		}
		return false;
	}
}

包装类

Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);//1.0
//因为有三元运算符,所以进行了自动类型提升
public void test(){
        Object o2;
        if (true)
            o2 = new Integer(1);
        else 
            o2 = new Double(2.0);
        System.out.println(o2);//1
    }
public void test3(){
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j);//false
        //引用类型所以比的是地址

        Integer m = 1;
        Integer n = 1;
        System.out.println(m == n);//true
    
        Integer x = 128;
        Integer y = 128;
        System.out.println(x == y);//false
        //包装类中IntegerCache -128~127都在同一个地址中,超出范围会新建一个对象所以地址不同返回false
}

单例设计模式

//饿汉式
class Hunger{
	//私有化构造器
	private Hunger(){
	
	}
	//内部创建类的对象
	//要求此对象必须声明为静态的
	private static Hunger instance = new Hunger();

	//提供公共的静态方法,返回类的对象
	public static Hunger getInstance(){
		return instance;
	}
}
//懒汉式
class Lazy{
	//私有化构造器
	private Lazy(){
	}
	//声明当前类对象,没有初始化
	//此对象也必须声明为static
	private static Lazy instance = null;
	//声明public、static的返回当前对象的方法
	public static Lazy getInstance(){
		if(instance == null){
			instance = new Lazy();
		}
		return instance;
	}
}

饿汉式:
坏处:对象加载时间过长
好处:饿汉式是线程安全

懒汉式:
好处:延迟对象的创建
当前写法坏处:线程不安全

数据库相关

1.有一个学校里面有班级,班级里有学生,每个学生可以选择多门课程,每个学生的课程有对应的成绩。现在需要查出各个班级的各个课程的平均成绩,请写出设计的表结构,和SQL。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值