扩展已知类的功能

扩展已知类的功能

1.采用子类

缺点:
a、与具体类相关联:
如果继承了该类,那么使用范围就会被固定
继承了Connection,只能在mysql数据库中使用,在其他数据库就使用不了了
b、有些不能继承
c、信息不全
 

2.采用包装类(装饰者设计模式)

装饰者设计模式的步骤:
1. 在装饰类的内部维护一个被装饰类的引用。
2. 让装饰类有一个共同的父类或者是父接口。
 
采用包装类扩展已知类的功能
口诀:
a. 写一个类,实现和被包装类相同的接口 (使他们具有相同的行为)
b. 创建一个实例变量,引用被包装类对象 (最好做到与具体类无关)
c. 编写一个构造函数,传入被包装类对象 (注入: DI)
d. 对于需要改写的方法,写自己的代码
e. 对于不需要改写的方法,引用被包装类的对象的对应方法即可

3.静态代理

 
Person:
package com.hcx.proxy;

public class Person {

	public void eat() {
		System.out.println("吃饭");
	}
	
	
	public void sleep(){
		System.out.println("睡觉");
	}
}

 
ProxyPerson(代理类):
package com.hcx.proxy;

public class ProxyPerson {

	private Person p ;
	
	public ProxyPerson(Person p) {
		this.p = p ;
	}
	
	public void eat() {
		p.eat() ;
	}
	
	
	public void sleep(){
		p.sleep() ;
		System.out.println("睡觉时间: " + System.nanoTime());
	}
	
}
Test:
package com.hcx.proxy;

public class Test {

	public static void main(String[] args) {
		ProxyPerson p = new ProxyPerson(new  Person()) ;
		
		p.eat() ;
		p.sleep() ;
	}
}

 

4.动态代理

 
Human:(Interface):
package com.hcx.proxy1;

public interface Human {

	public void eat() ;
	
	public void sing(float money) ;
	
	public void dance(float money) ;
}
SpringBrother:(来学习的人,实现humen接口)
package com.hcx.proxy1;

public class SpringBrother implements Human {

	@Override
	public void eat() {
		System.out.println("吃饭中......");
	}

	@Override
	public void sing(float money) {
		System.out.println("拿到钱: " + money + "开唱");
	}

	@Override
	public void dance(float money) {
		System.out.println("拿到钱: " + money + "跳舞");
	}

}
ProxyHuman:(代理的人,也要实现human接口)
package com.hcx.proxy1;

//模拟动态代理的原理
public class ProxyHuman implements Human {
	
	private Human man ;
	
	public ProxyHuman(Human man) {
		this.man = man ;
	}

	@Override
	public void eat() {
		man.eat() ;
	}

	@Override
	public void sing(float money) {
		if(money > 1000)
			man.sing(money/2) ;
	}

	@Override
	public void dance(float money) {
		if(money > 2000)
			man.sing(money/2) ;
	}

}
Test:
 
package com.hcx.proxy1;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		Human man = new SpringBrother() ;
//		
//		man.eat() ;
//		man.sing(100) ;
//		man.dance(200) ;
		
		Human man = new ProxyHuman(new SpringBrother()) ;
		
		man.eat() ;
		man.sing(2000) ;
		man.dance(4000) ;
		
	}

}

基于接口的动态代理

 
Human:
package com.hcx.proxy2;

public interface Human {

	public void eat() ;
	
	public void sing(float money) ;
	
	public void dance(float money) ;
}
SpringBrother:
package com.hcx.proxy2;

public class SpringBrother implements Human {

	@Override
	public void eat() {
		System.out.println("吃饭中......");
	}

	@Override
	public void sing(float money) {
		System.out.println("拿到钱: " + money + "开唱");
	}

	@Override
	public void dance(float money) {
		System.out.println("拿到钱: " + money + "跳舞");
	}

}
Test:
package com.hcx.proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//基于接口的动态代理
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		Human man = new SpringBrother() ;
//		
//		man.eat() ;
//		man.sing(100) ;
//		man.dance(200) ;
		
		final SpringBrother sb = new SpringBrother() ;
		
		Human man = (Human)Proxy.newProxyInstance(sb.getClass().getClassLoader(),
				sb.getClass().getInterfaces(), 
				new InvocationHandler() {
					
				/**
				 * invoke(Object proxy, Method method, Object[] args)
				 * proxy: 代理人
				 * method: 代理的方法
				 * args: 方法的参数
				 */
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						if(method.getName().equals("sing")){
							//说明要唱歌
							float money = (Float)args[0] ;
							if(money > 1000){
								Object retVal = method.invoke(sb, money/2) ;
								return retVal;
							}else
								return null ;
						}
						if(method.getName().equals("dance")){
							//说明要唱歌
							float money = (Float)args[0] ;
							if(money > 2000){
								Object retVal = method.invoke(sb, money/2) ;
								return retVal ;
							}else
								return null ;
						}
						Object ret =method.invoke(sb, args) ;
						return ret ;
						
					}
				}) ;
		
		man.eat() ;
		man.sing(1500) ;
		man.dance(2500) ;
	}

}

基于子类的动态代理:

采用第三方jar包实现动态代理:
cglib-nodep-2.1_3.jar
SpringBrother:
 
package com.hcx.proxy3;

public class SpringBrother {

	public void eat() {
		System.out.println("吃饭中......");
	}

	public void sing(float money) {
		System.out.println("拿到钱: " + money + "开唱");
	}

	public void dance(float money) {
		System.out.println("拿到钱: " + money + "跳舞");
	}

}
Test:
package com.hcx.proxy3;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

//基本子类的动态代理
public class Test {

	public static void main(String[] args) {
		/**
		 * Enhancer.create(type, callback): //创建一个代理对象
		 * type:是被代理对象的类型
		 * callback: 相当于Proxy类中的 invocationHandler 代理类要做的事
		 */
		final SpringBrother sb = new SpringBrother() ;
		SpringBrother h =  (SpringBrother)Enhancer.create(SpringBrother.class, new MethodInterceptor(){
		//创建的代理类的类型也是SpringBrother类型,因为在创建代理类的时候是以子类的形式创建的	
			//即创建的代理对象是被代理的子类
			@Override
			public Object intercept(Object arg0, Method method, Object[] args,
					MethodProxy arg3) throws Throwable {
				if(method.getName().equals("sing")){
					//说明要唱歌
					float money = (Float)args[0] ;
					if(money > 1000){
						Object retVal = method.invoke(sb, money/2) ;
						return retVal;
					}else
						return null ;
				}
				if(method.getName().equals("dance")){
					//说明要唱歌
					float money = (Float)args[0] ;
					if(money > 2000){
						Object retVal = method.invoke(sb, money/2) ;
						return retVal ;
					}else
						return null ;
				}
				Object ret =method.invoke(sb, args) ;
				return ret ;
			}
		}) ;
		
		
		h.eat() ;
		h.sing(1500) ;
		h.dance(2500) ;
	}
}


 







 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以这样定义Person和Student: ``` class Person: def __init__(self, name): self.name = name class Student(Person): def __init__(self, name, student_id): super().__init__(name) self.student_id = student_id ``` 然后创建对象: ``` p1 = Person("Tom") p2 = Person("Jerry") s1 = Student("Alice", "51") s2 = Student("Bob", "S2") ``` 其中p1和p2是Person的对象,s1和s2是Student的对象。注意,由于Student是Person的派生,因此s1和s2都可以被视为Person的对象。 ### 回答2: Person一个,而Student是Person的公用派生,表示学生的特性和行为。 假设p1和p2是Person的两个对象,表示两个不同的人。而s1和s2是Student的两个对象,表示两名学生。 Person可能包含属性(例如姓名、年龄)和方法(例如说话、走路),Student基于Person,可能添加了额外的属性(例如学号、班级)和方法(例如学习、做作业)。 我们可以通过以下方式创建和使用这些对象: 1. 创建p1和p2对象: ```cpp Person p1; Person p2; ``` 2. 创建s1和s2对象: ```cpp Student s1; Student s2; ``` 这样,p1、p2、s1和s2就代表了四个不同的对象,可以在程序中使用它们的属性和方法。 例如,可以在程序中为p1对象设置姓名和年龄,然后调用p1的说话方法: ```cpp p1.setName("张三"); p1.setAge(20); p1.speak(); ``` 对于学生对象,可以设置学号和班级,然后调用学生对象的学习方法: ```cpp s1.setStudentID("20210001"); s1.setClass("计算机科学班"); s1.study(); ``` 通过继承的关系,学生对象s1和s2既可以调用Person中的方法(例如说话),也可以调用Student中的方法(例如学习)。 当然,具体的实现细节会根据编程语言和程序的具体需求而有所不同。以上只是一个简单的示例,仅用于说明概念。在实际开发中,可能还会根据需求添加其他属性和方法,并进行更复杂的逻辑实现。所以,请根据具体的开发环境和需求进行实际操作。 ### 回答3: Person一个,而Student是Person的派生。那么意味着Student会继承Person的属性和方法。 有两个对象p1和p2是Person的实例,这些对象会拥有Person中定义的属性和方法。 另外,Student也有两个对象s1和s2是Student的实例,这些对象除了继承了Person的属性和方法外,还会有自己独有的属性和方法。 这样的继承关系可以理解为,Person是一个更一般的别,即父,而Student是Person的一个更具体的别,即子。 比如,Person中可能有name、age等属性和sayHello()等方法,p1和p2对象可以分别有不同的name、age属性,并且可以调用相同的sayHello()方法。 而Student中可能有school、grade等属性和study()等方法,s1和s2对象则继承了name、age等属性并可以调用sayHello()方法,并且还可以有不同的school、grade属性和调用stud()方法。 总结起来,通过继承和派生的关系,Student可以获得Person的属性和方法,并且还可以自己添加一些独有的属性和方法。这样的设计可以方便地重用代码,并且在需要更具体的别时可以扩展新的功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值