- 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[]{});
- }
- }*/