面向对象

it2023-09-10  68

1.多态/反射工厂

多态

父类类型指向子类对象接口指向实现类

反射工厂

public Object getObj() throws IOException, ClassNotFoundException { Properties properties = new Properties(); properties.load(Factory.class.getClassLoader().getResourceAsStream("com/company/test.properties")); String type = properties.getProperty("type"); Class<?> clz = Class.forName(type); System.out.println(clz); return clz; }

##分支工厂

public Object getObj2() throws IOException, ClassNotFoundException { Properties properties = new Properties(); properties.load(Factory.class.getClassLoader().getResourceAsStream("com/company/test.properties")); String type = properties.getProperty("type"); if("person".equals(type)){ return new Person(); }else if... return null; }

分支工厂如果要返回新的类型,需要修改代码增加if判断

classloader

读取文件(双亲委派)

双亲委派

classpath

.class文件所在的路径

2.==/equals/hashcode

==

比较地址(基本数据类型比较值)

equals

object类中直接调用==比较,写成方法是为了方便重写

hashcode

根据key?通过散列算法得到hash值,由于压缩输入了空间,会产生hash碰撞("Aa"和"BB"的hashcode相同)

3.static

唯一,共享,依附于类

4.堆/栈/常量池

JVM内存结构

5.形参的改变能否影响实参

改变引用,则无法影响实参。改变内容[堆区的对象部分],则可以影响实参。

6.final

final修饰类:不能被继承final修饰方法:不能被重写final修饰变量:引用不可变/内容可变
最新回复(0)