多态反射机制

package duotai;

class Customer{
    @SuppressWarnings("unused")
	private String account;
	@SuppressWarnings("unused")
	private String password;
	//有两个函数名称一样,但是系统可以接受,这叫做函数重载(静态多态性)
	//一个函数呈现为多种状态,系统能够根据参数来决定调用谁
	//三种情况:参数个数不同,参数个数相同类型不同,个数类型相同出现的顺序不同
	//静态是指:虽然函数名只有一个,但是要写代码要写多个
	public Customer()
	{
		System.out.println("构造函数1");
	}
	
	public Customer(String account, String password) {
		System.out.println("构造函数2");
		this.account=account;
		this.password=password;
	}
}


public class CustomerTest {
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		Customer cus1 = new Customer();//调用构造函数
		@SuppressWarnings("unused")
		Customer cus2 = new Customer("3213","1213");//调用构造函数
	}

}

package duotai;

//模块1调用一个对话框,让对话框显示出来

class Module1 {
	private Dialog dialog;

	public Module1(Dialog dialog) {
		this.dialog = dialog;
	}
	
	public void callDialog(){
		dialog.show();
	}

}

abstract class Dialog{
	public abstract void show();
}
class Dialog1 extends Dialog {
	public void show() {
		System.out.println("对话框1显示");
	}
}
//客户对Dialog1不满意想自己开发一个Dialog2,被Module1调用,不能改变Module1原代码

class Dialog2 extends Dialog{
	public void show() {
		System.out.println("对话框2显示");
	}
}


public class DaliogTest {

	public static void main(String[] args) {
		
		Dialog1 dia = new Dialog1();
		Module1 mod1 = new Module1(dia);
		
		mod1.callDialog();
	}

}

package duotai;

//动态多态性一般在继承时使用
abstract class Person{
	public abstract void printInfo();
}

class Student extends Person{
	public void printInfo() {
		System.out.println("学生打印");
	}
}

class Teacher extends Person{
	public void printInfo() {
		System.out.println("老师打印");
	}
}

public class StudentTest {
/*	public static void print(Student stu) {
		stu.printInfo();
	}

	public static void print(Teacher tea) {
		tea.printInfo();
	}*/
  public static void  print(Person p) {//父类的引用可以指向子类对象
	p.printInfo();
}
	public static void main(String[] args) {
		print(new Student()); 
	}

}

package fanshe;


public class Customer {
	private String account;
	private String password;

	public Customer() {
		System.out.println("构造函数1");
	}

	public Customer(String account, String password) {
		System.out.println("构造函数2");
		this.account = account;
		this.password = password;
	}

	public void printInfo() {
		System.out.println("账号:" + account + "密码" + password);
	}
}

package fanshe;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Test1 {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {

		String className = "fanshe.Customer";
		// className cus = new className();
		// 得到类的信息
		Class c = Class.forName(className);

		// 得到构造函数,就可以生成一个对象
		Constructor[] cons = c.getConstructors();
		for (int i = 0; i < cons.length; i++) {
			String str = cons[i].getName();
			System.out.println("名字:" + str);
			
			Class[] params = cons[i].getParameterTypes();// 得到参数类型
			for (int j = 0; j < params.length; j++) {
				String name = params[j].getName();
				System.out.println(name);
			}
		}
		//得到里面的成员函数(包括继承过来的),就可以调用成员函数
		Method[] met = c.getMethods();
		for (int i = 0; i < met.length; i++) {
			String str = met[i].getName();
			System.out.println("名字:" + str);
			
			Class[] params = met[i].getParameterTypes();// 得到参数类型
			for (int j = 0; j < params.length; j++) {
				String name = params[j].getName();
				System.out.println(name);
			}
		}
	}

}

package fanshe;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Test2 {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		/*
		 * 反射为配置文件改变模块行为提供了可能
		 */
		String className = "fanshe.Customer";
		Class c = Class.forName(className);
		//c.newInstance();表示调用不带参数的构造函数
		// 生成一个对象:用构造函数
		Constructor con1 = c.getConstructor(new Class[] {Class.forName("java.lang.String"),Class.forName("java.lang.String") });
		//Constructor con1 = c.getConstructor(new Class[] {});// 得到不带参数的构造函数
	
		// 生成对象
		Object obj = con1.newInstance(new Object[] {"222","111"});
		//Object obj = con1.newInstance(new Object[] {});// 表示不传入参数
		//怎样调用obj里面的方法
		Method met = c.getMethod("printInfo", new Class[]{});//得到方法
		met.invoke(obj, new Object[]{});//调用
		
		/*Customer cus = new Customer("222","111");
		cus.printInfo();*/
		
	}

}


/*public class Test2 {
	public static void main(String[] args) throws Exception {
		String className = "fanshe.Customer";
		Class c = Class.forName(className);
		Constructor con1 = c.getConstructor(new Class[] {Class.forName("java.lang.String"),Class.forName("java.lang.String") });
		Object obj = con1.newInstance(new Object[] {"222","111"});
		Method met = c.getMethod("printInfo", new Class[]{});
		met.invoke(obj, new Object[]{});
	}
}*/


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值