Objective-C使用动态绑定:即在运行时解析方法调用.
精细.
但是,为什么我不能做这样的事情:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Intercept the exception
@try
{
@throw [ NSException
exceptionWithName:@"Exception named ME!"
reason:@"Because i wanted to"
userInfo:nil ] ;
}
@catch( id exc ) // pointer to an exception object?
{
//NSLog( @"%@ : %@\n", exc.name, exc.reason ) ; // ILLEGAL: Request for member
// 'name' in something not a structure or union..
// If objective-c … 我在后面的代码中动态创建WPF元素,并且对于Grid我构建的每个行,它由a CheckBox和动态数字组成TextBoxes.需要的交互如下:
TextBoxes连续的所有值都为0,则将该CheckBox
IsChecked属性设置为true和禁用它.TextBoxes更改为0,则启用
CheckBox并设置IsChecked为false.CheckBox,则将所有关联设置TextBoxes
为0和禁用CheckBox我能够使用以下代码完成最后一部分的第一部分:
Binding setScoreToZeroIfIsNormalChecked = new Binding("IsChecked");
setScoreToZeroIfIsNormalChecked.Source = this.NormalCheckBoxControl;
setScoreToZeroIfIsNormalChecked.Converter = m_NormalCheckBoxJointScoresConverter;
tempJointScoreControl.JointScoreContainer.SetBinding(ContainerBase.SingleAnswerProperty, setScoreToZeroIfIsNormalChecked);
Run Code Online (Sandbox Code Playgroud)
和转换器:
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool && targetType == typeof(Answer))
{
if ((bool)value)
{
Answer answer = new Answer();
answer.Value …Run Code Online (Sandbox Code Playgroud) 在C++中,在动态绑定期间,请考虑以下示例...
class Base
{
virtual void fun()
{
cout<<"Base";
}
};
class Derived : Base
{
void fun()
{
cout<<"Derived";
}
};
int main()
{
Base *bptr;
Derived d;
bptr=&d;
bptr->fun();
}
Run Code Online (Sandbox Code Playgroud)
由于虚拟关键字/动态绑定的声明,上述函数的输出是"Derived".
根据我的理解,将创建一个包含虚函数地址的虚拟表(Vtable).在这种情况下,为派生类创建的虚拟表指向继承的虚拟表fun().并且bptr->fun()将会得到解决,bptr->vptr->fun();.这指向继承的基类函数本身.我不完全清楚如何调用派生类函数?
我知道重载使用静态绑定和覆盖使用动态绑定.但如果他们混在一起呢?根据本教程,要解析方法调用,静态绑定使用类型信息,而动态绑定使用实际的Object信息.
那么,在以下示例中是否发生了静态绑定以确定sort()要调用的方法?
public class TestStaticAndDynamicBinding {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
Parent p = new Child();
Collection c = new HashSet();
p.sort(c);
}
}
Run Code Online (Sandbox Code Playgroud)
.
public class Parent {
public void sort(Collection c) {
System.out.println("Parent#sort(Collection c) is invoked");
}
public void sort(HashSet c) {
System.out.println("Parent#sort(HashSet c) is invoked");
}
}
Run Code Online (Sandbox Code Playgroud)
.
public class Child extends Parent {
public void sort(Collection c) {
System.out.println("Child#sort(Collection c) is invoked");
}
public void sort(HashSet c) {
System.out.println("Child#sort(HashSet c) …Run Code Online (Sandbox Code Playgroud) java polymorphism overloading dynamic-binding static-binding
我需要对C++中的动态绑定做一些澄清.我对以下内容感到困惑:
在C中,您可以拥有一组函数指针并分配相同签名的不同函数,并根据索引调用它们; 这是动态绑定吗?
在C++中,您可以拥有一个基类指针数组,但是您可以通过将派生类对象地址分配给基类类指针并使用虚函数来调用派生类的不同函数,这是动态绑定吗?
哪个术语是正确的 - Dynamic binding或者Link-Time Binding?
我正在尝试创建一个应用程序,我需要在树视图结构中显示员工及其部门,如下所示:
我怎么能用WPF做到这一点?
我遇到了用C++编写的代码:
#include<iostream>
using namespace std;
class Base {
public:
virtual int fun(int i) { cout << "Base::fun(int i) called"; }
};
class Derived: public Base {
private:
int fun(int x) { cout << "Derived::fun(int x) called"; }
};
int main()
{
Base *ptr = new Derived;
ptr->fun(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Derived::fun(int x) called
Run Code Online (Sandbox Code Playgroud)
在以下情况中:
#include<iostream>
using namespace std;
class Base {
public:
virtual int fun(int i) { }
};
class Derived: public Base {
private:
int fun(int x) { …Run Code Online (Sandbox Code Playgroud) Java 中不允许这样做:
class A {
public void method() {}
}
class B extends A {
private void method() {}
}
Run Code Online (Sandbox Code Playgroud)
它会生成一个编译错误:
error: method() in B cannot override method() in A
attempting to assign weaker access privileges; was public
Run Code Online (Sandbox Code Playgroud)
然而,这在 C++ 中是允许的:
class A {
public:
virtual void method() {}
};
class B : public A {
private:
void method() {}
};
int main(void) {
A* obj = new B();
obj->method(); // B::method is invoked, despite it being private
}
Run Code Online (Sandbox Code Playgroud)
C++ …
从文档 "如果子类定义了与相同的签名在超类中一个静态方法的静态方法,那么在子类中的方法隐藏了一个超类中."
我理解方法隐藏和覆盖之间的区别.但是,很奇怪的是,子类隐藏了超类方法,因为如果你有以下内容:
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
调用超类的静态方法.但是通过隐藏的定义,子类中的方法隐藏了超类中的方法.我没有看到子类是如何"掩盖/隐藏"超类静态方法,因为超类的方法是实际调用的方法.
dynamic-binding ×10
c++ ×4
java ×3
polymorphism ×3
oop ×2
wpf ×2
c# ×1
datasource ×1
inheritance ×1
objective-c ×1
overloading ×1
private ×1
spring ×1
spring-boot ×1
treeview ×1