我希望能够使用javascript检测页面中的Vista IE7保护模式,最好是.我的想法是执行违反保护模式的行为,从而暴露它.目标是为IE7 Vista用户提供适当的站点帮助消息.
我在扩展类型(在F#中)有以下代码,它调用它继承的类的受保护方法(在C#中),但我得到了异常(见下文).这有解决方法吗?
let getPagereference id =
this.ConstructPageReference(id)
Run Code Online (Sandbox Code Playgroud)
无法访问成员或对象构造函数"ConstructPageReference".只能在声明类型中访问私有成员.受保护的成员只能从扩展类型访问,并且无法从内部lambda表达式访问.
更新:
我试过跟随但得到相同的结果
let getPagereference id =
base.ConstructPageReference(id)
Run Code Online (Sandbox Code Playgroud)
更新2(解决方案):
这是代码:
type MyNewType() =
inherit SomeAbstractType()
let getPagereference id =
base.ConstructPageReference(id)
override this.SomeMethod()=
let id = 0
let pr = getPagereference id
Run Code Online (Sandbox Code Playgroud)
这应该是这样的:
type MyNewType() =
inherit SomeAbstractType()
member this.ConstructPageReference(id) =
base.ConstructPageReference(id)
override this.SomeMethod()=
let id = 0
let pr = this.ConstructPageReference(id)
Run Code Online (Sandbox Code Playgroud) 除了对派生类可见或不可见的正常表达外,还有其他区别吗?
如果你让它更明显,它是否会占用更多或更少的记忆,它会减慢速度还是......?
为什么我可以使用反射来实例化内部受保护的类,而不是使用包级保护的内部类?我不认为任何一种都可以在包装外进行访问.
请考虑以下示例:
package dummy;
public class ClassContainer {
protected static class InnerProtected {
public InnerProtected() {}
}
static class InnerDefault {
public InnerDefault() {}
}
private class InnerPrivate {
public InnerPrivate() {}
}
}
package driver;
public class DriverClass {
public static void main(String[] args) throws Exception {
Class.forName("dummy.ClassContainer$InnerProtected").newInstance();
Class.forName("dummy.ClassContainer$InnerDefault").newInstance();
Class.forName("dummy.ClassContainer$InnerPrivate").newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这两个类位于不同的包中.
main中的第一行(实例化InnerProtected)起作用.
第二行(实例化InnerDefault)抛出此异常:
Exception in thread "main" java.lang.IllegalAccessException: Class driver.DriverClass can not access a member of class dummy.ClassContainer$InnerDefault with modifiers "public"
Run Code Online (Sandbox Code Playgroud)
由于驱动程序是与类定义不同的包,因此实例化类的尝试不应该失败吗?
(对于它的价值:试图实例化InnerPrivate失败,如我所料:
Exception in thread "main" …Run Code Online (Sandbox Code Playgroud) 可能重复:
objective-c中的受保护方法
声明私有属性的方法很简单.
您声明在.m文件中声明的扩展名中.
假设我想声明受保护的属性并从类和子类访问它.
这是我试过的:
//
// BGGoogleMap+protected.h
//
//
#import "BGGoogleMap.h"
@interface BGGoogleMap ()
@property (strong,nonatomic) NSString * protectedHello;
@end
Run Code Online (Sandbox Code Playgroud)
那是编译.然后我补充说:
#import "BGGoogleMap+protected.h"
@implementation BGGoogleMap ()
-(NSString *) protectedHello
{
return _
}
@end
Run Code Online (Sandbox Code Playgroud)
问题开始了.我似乎无法在原始的.m文件之外实现类扩展.Xcode将要求该括号内的东西.
如果我做
#import "BGGoogleMap+protected.h"
@implementation BGGoogleMap (protected)
-(NSString *) protectedHello
{
return _
}
@end
Run Code Online (Sandbox Code Playgroud)
我无法访问BGGoogleMap + protected.h中声明的_protectedHello的ivar
当然我可以使用常规类别而不是扩展名,但这意味着我不能拥有受保护的属性.
所以我该怎么做?
在我的子类中,我应该将继承的受保护的非虚方法称为this.Method()或base.Method()?
使用this将允许我使用相同名称的新方法轻松隐藏方法.base只有在确定只需要调用基类的实现时,才应该明确指定对方法的调用吗?
我很熟悉Spring AOP.正如我在春季文档http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html中所读到的那样,Spring AOP研究代理的概念.
在8.2.3.1支持的切入点指示符部分中,我找到了以下注释
由于Spring的AOP框架基于代理的特性,受保护的方法根据定义不会被拦截,既不用于JDK代理(这不适用),也不用于CGLIB代理(这在技术上可行,但不建议用于AOP).因此,任何给定的切入点都只能与公共方法匹配!
起初,我不相信它,所以我试图在不使用接口的情况下实现它,默认情况下所有方法都是公开的,并且对上述情况感到惊讶.由于代理类是建议/目标对象的子类,受保护的方法可以由子类访问,所以我认为受保护的方法可以正常工作.
有人可以告诉我为什么不拦截受保护的方法?我错过了什么吗?
考虑以下类:
public class Vehicle { ... }
public class Coverage { ... }
public class VehicleList : IEnumerable<Vehicle> { ... }
public class CoverageList : IEnumerable<Coverage> { ... }
public abstract class Quote
{
protected VehicleList vehicles;
protected CoverageList coverages;
internal Quote() { ... }
public IReadOnlyCollection<Vehicle> Vehicles
{
get { return this.vehicles.AsReadOnly(); }
}
public IReadOnlyCollection<Coverage> Coverages
{
get { return this.coverages.AsReadOnly(); }
}
...
}
public sealed class OhQuote : Quote
{
//needs to access protected fields
... …Run Code Online (Sandbox Code Playgroud) 简化的情况
public class A {
protected A() { }
protected A Make() { return new A(); }
}
public class B : A {
A a = new A(); //inaccessible due to protection level
B b = new B();
private B()
{
A c = new A();//inaccessible due to protection level
a = new A(); //inaccessible due to protection level
a = Make();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么使用A类保护的构造函数在B类中创建A的实例是不可能的?
在我看来,受保护的构造函数就像受保护的方法,因此应该可以在子类中运行它.
我设计了一个相当简单的例子来说明我的观点如下:
abstract class AbstractSuperClass {
protected def someFunction(num: Int): Int
def addition(another: AbstractSuperClass, num: Int): Int
}
class SubclassSquare extends AbstractSuperClass {
override protected def someFunction(num: Int): Int = num * num
override def addition(another: AbstractSuperClass, num: Int): Int =
someFunction(num) + another.someFunction(num)
}
Run Code Online (Sandbox Code Playgroud)
我在执行代码时收到以下错误消息。(当然,main函数是正确定义的。)
错误:(12, 33) 类 AbstractSuperClass 中的方法 someFunction 无法在 AbstractSuperClass 中访问 访问受保护的方法 someFunction 不允许,因为前缀类型 AbstractSuperClass 不符合发生访问的类子类 someFunction(num) + another.someFunction(num)
该方法addition导致了问题。该代码尝试访问AbstractSuperClass实例的受保护字段,但从我的角度来看,这里应该不会引起任何问题,因为SubclassSquare是AbstractSuperClass.
但是,我知道这里一定有我不明白的地方。我想知道如何更改我的代码以使其编译。好评。
protected ×10
inheritance ×4
c# ×3
java ×2
oop ×2
c++ ×1
constructor ×1
f# ×1
field ×1
javascript ×1
mode ×1
objective-c ×1
private ×1
proxy ×1
public ×1
reflection ×1
scala ×1
spring ×1
spring-aop ×1
virtual ×1
xcode4.5 ×1