关于C#虚拟和覆盖机制如何在内部工作的主题已经在程序员中讨论过死亡......但是在google上半小时之后,我找不到以下问题的答案(见下文):
使用简单的代码:
Run Code Online (Sandbox Code Playgroud)public class BaseClass { public virtual SayNo() { return "NO!!!"; } } public class SecondClass: BaseClass { public override SayNo() { return "No."; } } public class ThirdClass: SecondClass { public override SayNo() { return "No..."; } } class Program { static void Main() { ThirdClass thirdclass = new ThirdClass(); string a = thirdclass.SayNo(); // this would return "No..." // Question: // Is there a way, not using the "new" keyword and/or the "hide" // mechansim (i.e. not …
是abstract function xxx吗?
我刚做了一个测试,似乎表明私有方法也是虚拟的?
class a {
private function test()
{
echo 1;
}
}
class b extends a {
private function test()
{
echo 2;
}
public function call()
{
$this->test();
}
}
$instance = new b;
$instance->call();
Run Code Online (Sandbox Code Playgroud)
输出是 2
以下代码来自"Inside the C++ object model"一书
#include <iostream>
using namespace std;
class X{};
class Y: public virtual X{};
class Z: public virtual X{};
class A: public Y, public Z{};
int main()
{
cout<<sizeof(X)<<" "<<sizeof(Y)<<" "<<sizeof(Z)<<" "<<sizeof(A)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的电脑(Windows,VS2010)中,输出为:
1 4 4 8
这是我的问题
1,sizeof(X)= 1
该书说当X类型生成两个实例时,比如说xa和xb.编译在A中插入一个字节,以便xa和xb可以有不同的地址.我不太明白原因.
2,sizeof(Y)= 4
通过使用虚拟继承,我们还有一个额外的虚拟指针吗?我想这可能与多态中的虚拟指针不同.任何人都可以给我Y的内存布局吗?
谢谢!
public abstract class MyBase
{
public abstract bool MyProperty
{
get;
protected set;
}
}
public class MyClass : MyBase
{
public MyClass()
{
this.MyProperty = true;
}
public override bool MyProperty
{
get;
protected set;
}
}
Run Code Online (Sandbox Code Playgroud)
构造函数MyClass()导致CA2214:
不要在构造函数中调用可覆盖的方法.
这通常仅显示是否调用与构造函数在同一类中定义的虚方法.例如访问MyPropertyinside MyBase的构造函数.在这里,我调用非虚拟重写的实现在派生类的构造函数中继承的抽象属性.
为什么建议不要在虚拟基类中拥有数据成员?
功能成员怎么样?如果我有一个所有派生类共同的任务,虚拟基类可以执行任务,还是派生继承自两个类 - 从虚拟接口和执行任务的普通基类?
谢谢.
大多数修饰符对我来说都是有意义的 - 抽象的,受保护的等等.但"虚拟"对于真正"可覆盖"的东西来说似乎是一个令人困惑的任意选择.
计算中的"虚拟"(虚拟机,虚拟内存)似乎意味着某种东西的抽象表示.我猜这源于光学,其中虚拟图像(通常在*vert*ed中)本身来自中世纪拉丁语virtuālis,意思是"有效".
所以在我看来,如果计算中的"虚拟"方法是最重要的方法,而不是一个可以覆盖的方法,那将更有意义.
也许我错过了一些关于"虚拟"的基本信息,任何人都可以更清楚地了解它为何被选中?
关于虚拟基类继承的一些令我困惑的事情......给出以下类:
class A
{
virtual void foo() = 0;
}
class B : virtual A
{
void foo() { /* do X */ }
}
class C : virtual A
{
void foo() { /* do Y */ }
}
class D : B, C
{
}
Run Code Online (Sandbox Code Playgroud)
这会编译吗?如果是这样,以下代码的结果是什么:
D d;
A* a = &d;
a->foo();
Run Code Online (Sandbox Code Playgroud) 将System.Data.Entity组合添加到我的Web配置后,我遇到了这个错误:使用注册为allowDefinition ='MachineToApplication'的部分超出应用程序级别是错误的.此错误可能是由于未在IIS中将虚拟目录配置为应用程序引起的.
我删除了obj和bin文件夹,我删除了行身份验证="windows",尝试重新打开,因为有人说它有效,我检查了主文件夹中只有1个web.config(实体框架 - 文件夹形式,型号,DAL和BLL)......
还有什么其他原因会发生这种情况?我到处搜索,这基本上是我发现的上述原因....
这是我的web.config,如果它有所不同:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="CStringVKB" connectionString="Data Source=.;Initial Catalog=VKB;Persist Security Info=True;User ID=websiteservice;Password=websiteservice" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" optimizeCompilations="true" targetFramework="4.0" >
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<!--<authentication mode="Windows">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>-->
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager …Run Code Online (Sandbox Code Playgroud) 我正在学习c ++并且正在学习虚拟关键字.我已经在互联网上搜索,试图理解它无济于事.我进入我的编辑器并进行了以下实验,期望它打印出基本消息两次(因为我的印象是需要使用virtual关键字来覆盖函数).但是,它打印出两个不同的消息.有人可以向我解释为什么我们需要虚拟关键字,如果我们可以简单地覆盖函数并且看起来仍然看似多态行为?也许有人可以帮助我和其他人在未来理解虚拟与压倒一切.(我得到的输出是"我是基础",然后是"我是派生的").
#include <iostream>
using namespace std;
class Base{
public:
void printMe(){
cout << "I am the base" << endl;
}
};
class Derived: public Base{
public:
void printMe(){
cout << "I am the derived" << endl;
}
};
int main() {
Base a;
Derived b;
a.printMe();
b.printMe();
return 0;
}
Run Code Online (Sandbox Code Playgroud) virtual ×10
c++ ×4
inheritance ×4
c# ×3
overriding ×2
.net ×1
abstract ×1
asp.net ×1
directory ×1
methods ×1
php ×1
polymorphism ×1