第十四章 类型信息
程序员文章站
2022-05-07 10:54:13
...
.class和Class.forName()
有时候需要获得Class对象的引用,可以通过类名.class或者Class.forName("类名")来获得。两者的区别是,使用.class来创建对Class对象的引用时,不会自动地初始化该Class对象,而Class.forName()会。
RTTI和反射机制
RTTI:运行时类型信息(Run-Time Type Information)
RTTI和反射之间的区别在于,对RTTI来说,编译器在编译时打开和检查.class文件,而对反射机制来讲,.class文件在编译时是不可获取的,所以是在运行时打开和检查.class文件。
工厂方法
1. 简单工厂
public abstract class Shape {
private String name;
public abstract void draw();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Shape(String name) {
super();
this.name = name;
}
}
public class Circle extends Shape {
public Circle(String name) {
super(name);
}
@Override
public void draw() {
System.out.printf("draw %s.\n", this.getName());
}
}
public class Square extends Shape {
public Square(String name) {
super(name);
}
@Override
public void draw() {
System.out.printf("draw %s.\n", this.getName());
}
}
public class SimpleFactory {
public static Shape drawShape(String shapeType) {
if (shapeType == null || "".equals(shapeType))
return null;
if ("circle".equalsIgnoreCase(shapeType)) {
return new Circle(shapeType);
} else if ("square".equalsIgnoreCase(shapeType)) {
return new Square(shapeType);
}
return null;
}
// 测试方法
public static void main(String[] args) {
Shape circle = SimpleFactory.drawShape("circle");
circle.draw();
Shape square = SimpleFactory.drawShape("square");
square.draw();
}
}
2. 工厂方法
增加一个ShapeFactory抽象类,CircleFactory和SquareFactory均继承自ShapeFactory,来实现获取Shape的功能,分离出简单工厂中的SimpleFactory的if else判断。
public abstract class ShapeFactory {
public abstract Shape drawShape(String shapeType);
}
public class CircleFactory extends ShapeFactory {
@Override
public Shape drawShape(String shapeType) {
return new Circle(shapeType);
}
}
public class SquareFactory extends ShapeFactory {
@Override
public Shape drawShape(String shapeType) {
return new Square(shapeType);
}
}
public static void main(String[] args) {
ShapeFactory abstractDraw = new CircleFactory();
Shape circle = abstractDraw.drawShape("circle");
circle.draw();
}
3. 抽象工厂
新增加和Shape同一级别的Color,添加一个AbstractFactory来获取Shape和Color,添加一个FactoryProducer来获取ShapeFactory或ColorFactory。
public abstract class Color {
private String name;
public abstract void fill();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Color(String name) {
super();
this.name = name;
}
}
public class Blue extends Color {
public Blue(String name) {
super(name);
}
@Override
public void fill() {
System.out.printf("fill %s color.\n", this.getName());
}
}
public class Red extends Color {
public Red(String name) {
super(name);
}
@Override
public void fill() {
System.out.printf("fill %s color.\n", this.getName());
}
}
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shapeType);
}
public class ColorFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
if (color == null || "".equals(color))
return null;
if ("red".equalsIgnoreCase(color)) {
return new Red(color);
} else if ("blue".equalsIgnoreCase(color)) {
return new Blue(color);
}
return null;
}
@Override
public Shape getShape(String shapeType) {
return null;
}
}
public class ShapeFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
return null;
}
@Override
public Shape getShape(String shapeType) {
if (shapeType == null || "".equals(shapeType))
return null;
if ("circle".equalsIgnoreCase(shapeType)) {
return new Circle(shapeType);
} else if ("square".equalsIgnoreCase(shapeType)) {
return new Square(shapeType);
}
return null;
}
}
public class FactoryProducer {
public static AbstractFactory getFactory(String choice) {
if (choice == null || "".equals(choice))
return null;
if ("shape".equalsIgnoreCase(choice)) {
return new ShapeFactory();
} else if ("color".equalsIgnoreCase(choice)) {
return new ColorFactory();
}
return null;
}
public static void main(String[] args) {
AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");
Shape circle = shapeFactory.getShape("circle");
circle.draw();
AbstractFactory colorFactory = FactoryProducer.getFactory("color");
Color red = colorFactory.getColor("red");
red.fill();
}
}
推荐阅读
-
[Vue 牛刀小试]:第十四章 - 编程式导航与实现组件与 Vue Router 之间的解耦
-
php获取文件类型和文件信息的方法
-
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
-
Java 类型信息详解和反射机制介绍
-
.NET Core实战项目之CMS 第十四章 开发篇-防止跨站请求伪造(XSRF/CSRF)攻击处理
-
c++ primer(第五版)学习笔记及习题答案代码版(第十四章)重载运算与类型转换
-
python核心编程2 第十四章 练习
-
第十八天- 类型判断 函数和方法判断 反射
-
Python爬虫学习==>第十一章:分析Ajax请求-抓取今日头条信息
-
【.NET Core项目实战-统一认证平台】第十四章 授权篇-自定义授权方式