感谢关于继承的伟大答案.还有一个问题:
子类总是可以继承其超类的受保护成员.即使它们不在同一个包装中,这是真的吗?
鉴于代码:
class Foo
{
public:
Foo() {}
int foo() const { return 6; }
protected:
int foo() { return 5; }
};
int main(int argc, char* argv[])
{
Foo foo;
foo.foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
'Foo :: foo':无法访问类'Foo'中声明的受保护成员
在实际使用中,我正在尝试提供一个函数,该函数返回指向类所存储的对象的指针 - 可以为成员修改,而const则为其他所有人修改.我想如果我说的话
const Item *i = foo.foo();
Run Code Online (Sandbox Code Playgroud)
在非成员函数中,将调用正确的const变量.不知何故,编译器坚持访问受保护部分中的非const变体.知道为什么吗?谢谢.
我有这个基类创建一个新的SQL Server连接并在C#中做一些实用工具方法,我想在F#中继承.目前我无法从F#访问C#类中的受保护字段,尽管这可以在C#中使用.
C#抽象类
public abstract class SQL
{
protected readonly SqlConnectionStringBuilder bld;
protected readonly SqlCommand cmd;
protected readonly SqlConnection conn;
protected readonly string connstring;
protected string userid;
public SQL(string server, string db)
{
bld = new SqlConnectionStringBuilder();
bld.DataSource = server;
bld.InitialCatalog = db;
bld.IntegratedSecurity = true;
connstring = bld.ConnectionString;
conn = new SqlConnection(connstring);
cmd = new SqlCommand();
GetUserID();
//Other utility methods here
}
Run Code Online (Sandbox Code Playgroud)
要继承的F#代码
type Transfer ( server : string ) ( db : string ) =
inherit SQL(server, db) …Run Code Online (Sandbox Code Playgroud) 我有以下类层次结构
public class Third : Second
{
}
public class Second : First
{
}
public class First
{
private int MyVariable;
}
Run Code Online (Sandbox Code Playgroud)
如果我想从Second和Third访问MyVariable,我可以使MyVariable受到保护,但如果我想从Second而不是Third访问该怎么办.那可能吗?
我正在阅读Java中的protected modifier,哪些字段可以在同一个包和子类中访问.
现在我写了一些代码.
package com;
public class Parent {
protected void print()
{
System.out.println("dFDF");
}
}
Run Code Online (Sandbox Code Playgroud)
现在是子类.
package abstraact.concept;
import com.Parent;
public class BaseParent extends Parent{
public void printNum()
{
Parent p = new Parent();
p.print(); /** Getting error here */
// The method print() from the type Parent is not visible
}
public static void main(String[] args) {
BaseParent pp = new BaseParent();
pp.printNum();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我收到错误?因为受保护的方法/变量可以从子类访问.
我在基类中有一个受保护的方法:
public class BaseClass
{
protected virtual void Foo(){}
}
Run Code Online (Sandbox Code Playgroud)
该方法被覆盖了一个派生类:
public class Derived1 : BaseClass
{
protected override void Foo()
{
//some code...
}
}
Run Code Online (Sandbox Code Playgroud)
另一个派生类具有第一个派生类的实例.
当我尝试访问Foo方法(存在于基类中,如提到)时,我收到一个错误:
public class DerivedClass2 : BaseClass
{
BaseClass instance = new DerivedClass1();
instance.Foo(); // Here I get an error
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
Error CS1540: Cannot access protected member 'BaseClass.Foo' via a qualifier of type 'BaseClass';
the qualifier must be of type 'DerivedClass2' (or derived from it)
Run Code Online (Sandbox Code Playgroud)
我理解受保护的成员不应该放弃任何其他实例的值,即使是从相同类型派生的实例,
但有没有办法不将该方法修改为公共?
我正在研究jQuery UI Widget,但我遇到了一些问题.这是我实例化插件的方式.
$('#myId0, #myId1').mywidgetname();
Run Code Online (Sandbox Code Playgroud)
这是我的插件代码:
;(function ( $, window, document, undefined ) {
$.widget( "namespace.mywidgetname" , {
_create: function(){
debugger;
this._myPrivateProperty = 2;
....
Run Code Online (Sandbox Code Playgroud)
问题是,当调试器第二次停止时,我已经将this._myPrivateProperty实例化为值2.我认为该小部件将被实例化两次,但事实并非如此.
另外,如果我为每个元素指定不同的选项,如下所示:
$('#myId0').mywidgetname({myVarA: 'someText'});
$('#myId1').mywidgetname({myVarB: 'someText'});
Run Code Online (Sandbox Code Playgroud)
变量this.options对于每个变量都是不同的.现在有人如何将"私人"财产私有化?
谢谢
不幸的是,我主要在Java的背景下研究了类设计和设计模式; 因此,我有时很难将熟悉的模式转换为C++.
假设我们想要一个基类,其功能由子类扩展.在Java中,我们会这样做:
public class BaseClass<T> {
//T is used here
protected int foo = 0;
}
public class DerivedClass<T> extends BaseClass<T> {
public void incr_foo() {
++foo;
}
}
Run Code Online (Sandbox Code Playgroud)
我直接翻译成C++:
template<class T>
class BaseClass {
protected:
size_t foo = 0;
//T is used here
};
template<class T>
class DerivedClass : public BaseClass<T> {
public:
void incr_foo() {
++(this->foo);
}
};
Run Code Online (Sandbox Code Playgroud)
由于'protected'的C++语义来自Java语义,我们需要使用类似'this-> foo'的东西来访问基类的受保护成员.
编辑:如果我们只使用'++ foo',编译器会给出错误:'使用未声明的标识符foo'.
EditEnd
如果您需要访问基类的几个成员,这可能会有点乏味,并且不太好阅读.
这是一个很好的设计决定吗?如果没有,有什么更好的方法来实现这一目标?
我用这种方式定义了一个类:
package prueba;
public class OtraClase {
[...]
protected int num3;
[...]
Run Code Online (Sandbox Code Playgroud)
另一个类以这种方式定义:
package otro;
import prueba.*;
public class OtraClaseMas extends OtraClase{
Run Code Online (Sandbox Code Playgroud)
但是如果在最后一个类中我创建了一个OtraClase对象,我不能做这样的事情:
createdObjectOfOtraClase.num3=1;
Run Code Online (Sandbox Code Playgroud)
而且我认为根据我应该能够在这里提供的文档.它表示protected修饰符允许其类的子类在另一个包中进行访问.而且,就我看它而言,我并不认为它是另一个东西,而不是另一个包中它的类的子类.
我误会了什么吗?
编辑:我要么使用类的构造函数,要么使用另一个不同的函数,它在两个地方都不起作用.
构造函数的代码:
public OtraClaseMas(int num, int num2,int num3)
{
super(num, num2,num3);
OtraClase oc=new OtraClase(1,1,1);
//oc.num3=1; This doesn't work
}
Run Code Online (Sandbox Code Playgroud)
方法代码:
public void foo()
{
OtraClase oc=new OtraClase(1,1,1);
//oc.num3=1; This doesn't work
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有内部受保护类的基本泛型类.如何从基类继承并访问受保护的内部类?
作为示例,此代码将无法编译:
unit uFoo;
interface
type
TFoo<T> = class
protected
type
TFooProtected = class
end;
end;
TFoo2<T> = class(TFoo<T>)
protected
item: TFooProtected;
end;
Run Code Online (Sandbox Code Playgroud) protected ×10
inheritance ×6
java ×3
c# ×2
c++ ×2
subclass ×2
delphi ×1
f# ×1
factory ×1
jquery ×1
jquery-ui ×1
overloading ×1
overriding ×1
variables ×1
widget ×1