- fda
- dfa
- 第三题u
package net.mindview.typeinfo.test4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
abstract class Shape {
void draw(){
/*
* 重点在这里:
* this指代的是实例对象,由于使用+连接字符串,会自动调用对象的toString()方法.
*/
System.out.println(this + ".draw()");
}
public abstract String toString();
}
Circle extends Shape{
@Override
public String toString() {
return Circle;
}
}
Square extends Shape{
@Override
Square Triangle extends Shape{
@Override
Triangle//菱形
Rhomboid extends Shape{
@Override
Rhomboid Shapes {
static main(String[] args) {
List<Shape> shapes = new ArrayList<Shape>(Arrays.asList(
new Circle(),new Square(),1)">new Triangle(),1)">new Rhomboid()));
for(Shape shape:shapes){
shape.draw();
}
for(int i=0;i<shapes.size(); i++){
Shape shape = shapes.get(i);
if(i == 3){
Rhomboid r = (Rhomboid)shape;
Circle c = (Circle)shape;
}
}
}
}
运行结果:
Circle.draw()
Square.draw()
Exception in thread "main" Triangle.draw()
Rhomboid.draw()
java.lang.ClassCastException: net.mindview.typeinfo.test4.Rhomboid cannot be cast to net.mindview.typeinfo.test4.Circle
at net.mindview.typeinfo.test4.Shapes.main(Shapes.java:63)
不可以向下转型到Circle
-
第四题
Shapes {
){
添加instanceof判断
if (shape instanceof Rhomboid){
Rhomboid r = (Rhomboid)shape;
}
(shape instanceof Circle){
Circle c = (Circle)shape;
}
}
}
}
}
?
使用Class.newInstance方法,必须有一个无参构造方法
- 第五题:
package net.mindview.typeinfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
rotate(){
Class<? extends Shape> clazz = this.getClass();
if(!clazz.getSimpleName().equals()){
System.out.println(旋转"+ );
}else{
System.this+不需要旋转);
}
}
(Shape shape:shapes){
shape.draw();
shape.rotate();
}
for(int i=0;i<shapes.size(); i++){
Shape shape = shapes.get(i);
if(i == 3){
Rhomboid r = (Rhomboid)shape;
Circle c = (Circle)shape;
}
}
}
}
?
-
- 第七题
package net.mindview.typeinfo.test8;
import java.util.HashSet;
import java.util.Set;
interface A {}
B {}
C {}
D implements B{
private int di=0;
private String dname=""static{
System.this is D);
}
}
E extends D implements A,B,C{
int ei=private String ename=this is E F extends E implements A {
int fi=private String fname=this is F);
}
}
*
* 接受任意对象作为参数,递归打印出该对象继承体系中所有的的类.
* 包括继承的父类,和实现的接口
*
* 分析: 一个类智能继承一个父类,可以实现多个接口.
* 父类可以继承一个类,实现多个接口. 实现的接口可能和子类重复,因此需要去重.
* 递归实现
* @author samsung
*
*/
G {
printSuperClass(Class c){
if(c == null) return ;
System.out.println(c.getName());
得到这个类的接口
Class[] interfaces = c.getInterfaces();
(Class interfaceClass:interfaces){
printSuperClass(interfaceClass);
}
printSuperClass(c.getSuperclass());
}
}
Test8 {
main(String[] args) {
G g = G();
g.printSuperClass(F.);
}
}
运行结果:
net.mindview.typeinfo.test8.F
net.mindview.typeinfo.test8.A
net.mindview.typeinfo.test8.E
net.mindview.typeinfo.test8.A
net.mindview.typeinfo.test8.B
net.mindview.typeinfo.test8.C
net.mindview.typeinfo.test8.D
net.mindview.typeinfo.test8.B
java.lang.Object
?
- 第八题
package net.mindview.typeinfo.test8;
D {
F extends E {
printSuperClass(Class c){
Class upClass = c.getSuperclass();
try {
System..println(upClass.newInstance());
} catch (InstantiationException e) {
System.实例化Instance 失败);
System.exit(1);
} (IllegalAccessException e) {
System.);
}
if(upClass.getSuperclass() != null ){
printSuperClass(upClass);
}
}
}
this is D
E
net.mindview.typeinfo.test8.E@17cb0a16
net.mindview.typeinfo.test8.D@1303368e
java.lang.Object@37f2ae62
?
- 第九题
package net.mindview.typeinfo.test9;
import java.lang.reflect.Field;
public String name= c.getSuperclass();
{
获取类中的字段
Field[] fs = upClass.getDeclaredFields();
System..println(fs.length);
(Field f:fs){
打印字段名
System..println(f.getName());
获取字段值
Object value = upClass.getDeclaredField(f.getName());
System..println(value);
}
} (Exception e1) {
e1.printStackTrace();
}
Test9 {
);
}
}
运行结果:
2
i
int net.mindview.typeinfo.test9.E.i
name
java.lang.String net.mindview.typeinfo.test9.E.name
E
net.mindview.typeinfo.test9.E@1440578d
net.mindview.typeinfo.test9.D.i
name
java.lang.String net.mindview.typeinfo.test9.D.name
net.mindview.typeinfo.test9.D@26f04d94
java.lang.Object@38a3c5b6
?
- 第十题
package net.mindview.typeinfo.test10;
Pot{}
Test10 {
judgeType(Object c){
Class arrType = c.getClass().getComponentType();
System..println(arrType.getName());
}
main(String[] args) {
judgeType(new char[10]);
judgeType(new String[long[new boolean[new Pot[]);
}
}
运行结果:
char
java.lang.String
long
boolean
net.mindview.typeinfo.test10.Pot
?
- f
- asf
- a
- 第十四题
package net.mindview.typeinfo.test14;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import net.mindview.typeinfo.factory.Factory;
*
* 部件类
*
* 比如: 有过滤器部件,皮带部件等.
* 空气净化器中需要有过滤器部件,因此要有一个制造空气净化器的过滤器的工厂
* 汽车尾气净化器也需要有过滤器部件,因此需要一个制造汽车尾气的过滤器的工厂
*
* 皮带
* 车轮需要皮带,因此需要一个制造车轮的皮带的工厂
* @author samsung
*
Part{
@Override
return .getClass().getSimpleName();
}
目前已注册的部件工厂
static List<String> partNames = Arrays.asList(
net.mindview.typeinfo.test14.FuelFilter,net.mindview.typeinfo.test14.AirFilternet.mindview.typeinfo.test14.CabinFilternet.mindview.typeinfo.test14.OilFilternet.mindview.typeinfo.test14.FanBeltnet.mindview.typeinfo.test14.GeneratorBeltnet.mindview.typeinfo.test14.PowerSteeringBelt
);
{
}
static Random rand = new Random(47);
随机得到一个已注册的部件工厂,并制造部件
Part createRandom(){
int n = rand.nextInt(partNames.size());
return (Part) Class.forName(partNames.(n)).newInstance();
} (InstantiationException e) {
e.printStackTrace();
} (IllegalAccessException e) {
e.printStackTrace();
} (ClassNotFoundException e) {
e.printStackTrace();
}
过滤器部件
Filter extends Part {
}
燃料过滤器部件
FuelFilter extends Filter {
class Factory implements net.mindview.typeinfo.factory.Factory<FuelFilter>{
@Override
FuelFilter create() {
FuelFilter();
}
}
}
空气净化器部件
AirFilter extends Filter {
class Factory implements net.mindview.typeinfo.factory.Factory<AirFilter>{
@Override
AirFilter create() {
AirFilter();
}
}
}
机舱空气过滤器部件
CabinFilter extends Filter {
class Factory implements net.mindview.typeinfo.factory.Factory<CabinFilter> CabinFilter create() {
CabinFilter();
}
}
}
燃油过滤器部件
OilFilter extends Filter {
class Factory implements net.mindview.typeinfo.factory.Factory<OilFilter> OilFilter create() {
OilFilter();
}
}
}
皮带部件
Belt extends Part{}
风扇皮带部件
FanBelt extends Belt {
class Factory implements net.mindview.typeinfo.factory.Factory<FanBelt> FanBelt create() {
FanBelt();
}
}
}
发动机皮带部件
GeneratorBelt extends Belt {
class Factory implements net.mindview.typeinfo.factory.Factory<GeneratorBelt> GeneratorBelt create() {
GeneratorBelt();
}
}
}
转向动力装置皮带部件
PowerSteeringBelt extends Belt {
class Factory implements net.mindview.typeinfo.factory.Factory<PowerSteeringBelt> PowerSteeringBelt create() {
PowerSteeringBelt();
}
}
}
*
* 查询目前已注册的工厂类
* @author samsung
*
RegisteredFactories {
main(String[] args) {
0;i<10;i++){
System..println(Part.createRandom());
}
}
}
?
- 第十五题: 内容太多,直接参考demo
- af
- a
- 第十八题:
package net.mindview.typeinfo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.regex.Pattern;
ShowMethods {
private ShowMethods(){}
static String usage = ""
+ usage:n"
+ ShowMethods qualified.class.namenTo show all methods in class or:nShowMethods qualified.class.name. wordnTo search for methodds invoiving 'word';
static Pattern p = Pattern.compile(w+.);
if(args.length<.println(usage);
System.exit();
}
int lines = ;
{
Class<?> c = Class.forName(args[]);
getMethods获取的是整个继承树中所有的方法
Method[] methods = c.getMethods();
获取已有的构造器
Constructor[] ctors = c.getConstructors();
if(args.length == 打印所有继承树中的方法名
(Method method: methods){
System.out.println(p.matcher(method.toString()).replaceAll());
}
打印全部构造器
(Constructor ctor: ctors){
System.out.println(p.matcher(ctor.toString()).replaceAll());
}
lines = methods.length + ctors.length;
} {
打印指定类中的方法
(Method method: methods){
if(method.toString().indexOf(args[1]) != -){
System.));
lines++;
}
}
打印构造器
(Constructor ctor :ctors){
if(ctor.toString().indexOf(args[1])!=-;
}
}
}
} (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
?
- af
- a
- f
- af
- a
- fa
- f