是在一个子类中重新定义一个非静态方法,具有相同的一切,但作为静态覆盖或隐藏它?
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html说隐藏.但是当我将超类方法声明为final时,我得到一个覆盖错误.
超类声明是
final static void display() { ... }
Run Code Online (Sandbox Code Playgroud)
子类:
void display() { ... }
Run Code Online (Sandbox Code Playgroud)
给出覆盖错误.
我有点腌渍.我知道从块中调用[self methodName]会导致保留周期.
但是在这个类中由于多线程,我不能允许执行块从块以外的任何其他地方访问的方法,因为它可能会导致严重的问题.
当前代码:
if (_getRunning==NO){
__weak SyncArrayMT *_weak_self = self;
_get_t = ^void (void){
_weak_self->_getRunning = YES;
NSArray *objects = [_weak_self get:getQuery
usingClassCtor:ctor
withAuthBlock:authBlock];
if (_weak_self.getBlockCb)
_weak_self.getBlockCb(objects);
_weak_self->_getRunning = NO;
};
}
Run Code Online (Sandbox Code Playgroud)
确实如此,它称之为[self getmethod].虽然调度块可以运行此方法,但我不希望此类之外的任何内容调用此方法.那么,可以这样覆盖这个继承的方法:
- (NSArray *) get:(NSString *)getQuery usingClassCtor:(initBlock)initCb withAuthBlock:(authenticate)authBlock
{
NSLog(@"Direct call to get is not allowed - use the threaded method");
return nil;
}
Run Code Online (Sandbox Code Playgroud)
然后将块更改为:
_get_t = ^void (void){
_weak_self->_getRunning = YES;
NSArray *objects = [super get:getQuery
usingClassCtor:ctor
withAuthBlock:authBlock];
if (_weak_self.getBlockCb)
_weak_self.getBlockCb(objects);
_weak_self->_getRunning = NO;
}; …Run Code Online (Sandbox Code Playgroud) objective-c superclass method-hiding objective-c-blocks retain-cycle
如果我有这样的事情:
class Base
{
public void Write()
{
if (this is Derived)
{
this.Name();//calls Name Method of Base class i.e. prints Base
((Derived)this).Name();//calls Derived Method i.e prints Derived
}
else
{
this.Name();
}
}
public void Name()
{
return "Base";
}
}
class Derived : Base
{
public new void Name()
{
return "Derived";
}
}
Run Code Online (Sandbox Code Playgroud)
并使用以下代码进行调用,
Derived v= new Derived();
v.Write(); // prints Base
Run Code Online (Sandbox Code Playgroud)
然后Name调用基类的方法。但是this该Write方法中关键字的实际类型是什么?如果那是Derived类型(如Program控件在Write方法中输入第一个if块),则它正在调用基本Name方法,为什么显式强制(Derived)this转换将调用更改 …
我有两节课如下:
public class Car {
public static void print() {
System.out.println(getWord());
}
public static String getWord() {
return "CAR";
}
}
public class BMW extends Car {
public static String getWord() {
return "BMW";
}
}
// main method
public static void main(String args[]) {
BMW.print();
}
Run Code Online (Sandbox Code Playgroud)
运行上面的示例后,打印此输出:
汽车
我的问题是:为什么方法getWord()不被覆盖?
这些OCA练习题(pdf)中的问题3 :
abstract class Writer {
public static void write() {
System.out.println("Writing...");
}
}
class Author extends Writer {
public static void write() {
System.out.println("Writing book");
}
}
public class Programmer extends Writer {
public static void write() {
System.out.println("Writing code");
}
public static void main(String[] args) {
Writer w = new Programmer();
w.write();
}
}
Run Code Online (Sandbox Code Playgroud)
输出为Writing...。
我不明白为什么。作为Programmer重写Writer的write方法,我认为它应该在中Programmer而不是在中调用该方法Writer。
为什么?
这是我的超类:
class Superclass {
int a=89;
final static void m( int p){
System.out.println("Inside superclass");
}
static void n(){
system.out.print("superclass");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的子类::
class Subclass extends Superclass {
int a=90;
static void m( int p){
System.out.println("Inside subclass");
}
static void n(){
system.out.print("subclass");
}
}
Run Code Online (Sandbox Code Playgroud)
主要类:
class main {
public static void main(String[] args) {
Subclass.m(89);
new Subclass().n();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不明白为什么 Javac 在静态方法中给我覆盖错误..一个 PS plzz 详细说明所有覆盖规则对于隐藏也是有效的。喜欢
新的方法定义必须具有相同的方法签名(即方法名称和参数)和相同的返回类型。覆盖方法中的参数是否应该是 final 由子类方法的签名决定,不包含参数的 final 修饰符,只包含它们的类型和顺序。
新的
方法定义不能缩小方法的可访问性,但可以扩大方法的可访问性
我的错误是: 运行:
线程“main”中的异常 java.lang.RuntimeException: 不可编译的源代码 - javaapplication3.Subclass 中的 m(int) …
我收到以下编译器警告:
'Resources.Foo.GetType()' hides inherited member 'object.GetType()'. Use the new keyword if hiding was intended. Resources.NET\Resources\Class1.cs 123 20 Resources
Run Code Online (Sandbox Code Playgroud)
对于这个(非常简化的)代码:
public interface IFoo
{
int GetType();
string GetDisplayName();
}
public class Foo : IFoo
{
public int GetType() { return 3; } *** Warning: points to here (line 123)***
public string GetDisplayName() { return "Foo"; }
}
Run Code Online (Sandbox Code Playgroud)
注意,我没有得到的是为什么GetDisplayName()函数没有得到警告.我显然忽略了一些东西.
我已经探讨了Stack Overflow并搜索了这个错误,它似乎适用于类继承,而不是接口.我真的很困惑为什么会为接口触发它(它将其方法定义为虚拟).
提前感谢您的见解.
我有一个基类,它有一个方法将文件移动到适当的文件夹.有许多不同的文件有许多不同的命名方案.每个文件的移动和文件夹创建都是相同的,但由于文件名不同,确定日期也不同.我想这样做:
public class FileBase
{
protected FileInfo _source;
protected string GetMonth()
{
// 2/3 Files have the Month in this location
// So I want this to be used unless a derived class
// redefines this method.
return _source.Name.Substring(Source.Name.Length - 15, 2);
}
public void MoveFileToProcessedFolder()
{
MoveFileToFolder(Properties.Settings.Default.processedFolder + GetMonth);
}
private void MoveFileToFolder(string destination)
{
....
}
}
public class FooFile : FileBase
{
protected new string GetMonth()
{
return _source.Name.Substring(Source.Name.Length - 9, 2);
}
}
public class Program …Run Code Online (Sandbox Code Playgroud) 我试图更好地理解c#中的抽象类.
我知道在抽象类中你必须覆盖抽象方法并且可以覆盖虚方法.
我的问题是:
我可以覆盖非虚拟方法吗?(我知道通常我不能 - 但也许抽象类不同?)或者它会隐藏吗?
另外,正如我在这里读到的如何强制调用C#派生方法(第一个答案) - 在我看来,因为非虚拟方法在编译时静态链接而且无法更改 - 我无法调用方法在派生类中,从来没有?如果是这样 - 隐藏方法有什么意义?
我需要根据传递给c#中构造函数的参数隐藏类中的几个方法.我该怎么做?提前致谢!
更多信息:我参与了这个GUI开发,他们拥有一个API,可以访问硬件中的寄存器.现在他们正在发布一个更新的硬件,所以我需要支持新旧的硬件(旧的一个具有较旧的删除和一些新的寄存器).
此外,我需要保留一个名为"API"的类,就像我们在很多地方使用它一样.因此,排除了使用具有不同名称的较新API的想法.
最后,我得到了这个想法,将新版本包含在旧版本中,只是有条件地隐藏了注册表访问方法.
在下面的代码中:
请告诉我为什么只调用派生类成员函数,是因为hiding吗?以及为什么在 中没有发生错误b.func(1.1);,参数是int x。
#include <bits/stdc++.h>
using namespace std;
class A {
public:
virtual int func(float x)
{
cout << "A";
}
};
class B : public A {
public:
virtual int func(int x)
{
cout << "B";
}
};
int main()
{
B b;
b.func(1); // B
b.func(1.1); // B
return 0;
}
Run Code Online (Sandbox Code Playgroud) method-hiding ×11
c# ×5
java ×4
inheritance ×3
overriding ×3
c++ ×1
constructor ×1
function ×1
interface ×1
objective-c ×1
oop ×1
overloading ×1
retain-cycle ×1
static ×1
superclass ×1