我可以在java中的抽象基类A中实现抽象方法吗?
如果答案是肯定的,并且在基类A中存在实现的抽象方法,并且存在来自A的派生类B(B不是抽象的).B仍然必须实现该基本抽象方法吗?
我觉得我的问题非常愚蠢,或者另一种说法是:我在代码中迷失了,现在看到一个解决方法.对问题保持太长时间,你的视力变得更窄更窄> <.另外,我对继承,多态等等还不够好
这是一个想法:我有多个派生类列表,我想在这些列表上调用泛型函数(访问和修改基类的成员).我觉得与继承有关,但我无法让它现在正常运作.
这是我打算做的一个非常简单的例子:
class Baseclass
{
public int ID;
public string Name;
}
class DerivedClass1 : Baseclass
{
}
private void FuncOnBase(List<Baseclass> _collection)
{
// ...
foreach (Baseclass obj in _collection)
{
++obj.ID;
}
// ...
}
private void FuncTest()
{
List<DerivedClass1> collection1 = new List<DerivedClass1>();
collection1.Add(new DerivedClass1() { ID = 1 });
collection1.Add(new DerivedClass1() { ID = 2 });
collection1.Add(new DerivedClass1() { ID = 3 });
FuncOnBase(collection1); // ==> forbidden, cannot convert the derived class list to the …Run Code Online (Sandbox Code Playgroud) 例如,我有这样的课程:
class Base
{
public: void SomeFunc() { std::cout << "la-la-la\n"; }
};
Run Code Online (Sandbox Code Playgroud)
我从中得到了一个新的:
class Child : public Base
{
void SomeFunc()
{
// Call somehow code from base class
std::cout << "Hello from child\n";
}
};
Run Code Online (Sandbox Code Playgroud)
我希望看到:
la-la-la
Hello from child
Run Code Online (Sandbox Code Playgroud)
我可以从派生类调用方法吗?
class A {
public:
template<typename T> void func(size_t n, T values[]) { ... }
};
class B : public A {
public:
void func(size_t n, uint32_t values[]) { ... }
};
Run Code Online (Sandbox Code Playgroud)
调用此代码时,为什么函数B::func() 不优先于函数模板A::func()?
uint32_t values[5];
A* obj = new B();
obj->func(5, values);
Run Code Online (Sandbox Code Playgroud) 为什么我不能base在f这里调用实现:
type Base =
abstract f : int -> int -> int
default this.f (x : int) (y : int) : int = x + y
type Derived =
inherit Base
override this.f (x : int) (y : int) : int = base.f -x -y
Run Code Online (Sandbox Code Playgroud)
调用base.f引发此编译器错误:
error FS0419: 'base' values may only be used to make direct calls to the base implementations of overridden members
Run Code Online (Sandbox Code Playgroud)
如果我f改为采用单个参数,那么它就会编译.据推测,这与curried参数与tupled参数有关,但上面的代码对我来说很好.
(抱歉模糊的标题)
我有一个基类B与各种派生类D1,D2等.
此外,我有一个模板
template <class T> Storage;
Run Code Online (Sandbox Code Playgroud)
现在,每个派生类都有不同版本的Storage类
class D1 : public class B {
Storage<int> *myStorage;
void action1();
void specific_D1_action();
// other stuff...
}
class D2 : public class B {
Storage<float> *myStorage;
void action1();
void specific_D2_action();
// other stuff...
}
Run Code Online (Sandbox Code Playgroud)
D1::action1()并D2::action1()使用他们各自myStorage的代码和完全相同的代码.但是,各个myStorage对象的实际模板参数不同.
是否有可能为所有版本的存储都有一种"基类",以便我可以在基类B(而不是D1,D2,...)中定义myStorage和action1以避免代码重复?
我认为我不能"模糊"基类B,因为派生类中的特定操作.
(这个例子非常简单 - 在实际情况下,模板参数是复杂的类)
我正在进行Java练习,无法弄清楚我做错了什么.我创建了一个Movie类(带有变量:rating,title,movieId和FEE_AMT的常量),然后使用:Action,Comedy和Drama扩展了类.这些派生类没有其他变量,只有不同的FEE_AMT.
在Movie(和派生类)中,有一种方法可以计算到期的滞纳金:
/**
* Returns the late fee due on a Movie rental
* @param days the number of days for late fee calculations
* @return true or false depending on the evaluation of the expression
**/
public double calcLateFees(int days){
return(days * FEE_AMT);
}
Run Code Online (Sandbox Code Playgroud)
如果我只是直接用对象调用方法,例如:comedy1.calcLateFees(2) - 它将根据派生方法中的不同常量值生成正确的费用金额.
现在我需要创建一个Rental类并main()创建一个类型租赁类的数组来保存Rental对象(由Movie对象,renterId和daysLate组成).以下是接收Rental对象数组的方法,并返回数组中所有租借的滞纳金:
/**
* lateFeesOwed returns the amount of money due for late fees on all movies
* which are located in an array of Rentals.
*
* …Run Code Online (Sandbox Code Playgroud) 我有一个基类Base,一个SpecializedBase从Base派生的更专业的类,以及后者的子类,如Derived.
我实现虚拟功能SpecialisedBase是纯虚拟Base.如何确保此功能没有超载Derived?
我有两节课:
第一:
class Thing {
public:
int code;
string name;
string description;
int location;
bool canCarry;
Thing(int _code, string _name, string _desc, int _loc, bool _canCarry) {
code = _code;
name = _name;
description = _desc;
location = _loc;
canCarry = _canCarry;
}
};
Run Code Online (Sandbox Code Playgroud)
第二:
class Door: public Thing {
private:
bool open;
public:
int targetLocation;
Door(int _code, string _name, string _desc, int _loc, int _targetLoc) :
Thing(_code, _name, _desc, _loc, false) {
open = false;
targetLocation = _targetLoc;
} …Run Code Online (Sandbox Code Playgroud) 我有下面的c ++ 11代码,可以正常工作,而我希望它会崩溃甚至无法编译。检索指向纯虚拟成员函数的指针应返回null或无效的指针,或者应被编译器阻止。我想了解它为什么起作用。
我知道还有其他(更好)的方式对此进行编码,这是理解语法的纯理论性问题。
#include <iostream>
#include <functional>
class Abstract
{
public:
void foo()
{
auto func = std::bind(&Abstract::virtualFoo, this);
func();
}
protected:
virtual void virtualFoo() = 0;
};
class Derived1 : public Abstract
{
private:
void virtualFoo() override
{
std::cout << "I am Derived1\n";
}
};
class Derived2 : public Abstract
{
private:
void virtualFoo() override
{
std::cout << "I am Derived2\n";
}
};
int main(int argc, char *argv[])
{
Abstract * a1 = new Derived1;
Abstract * a2 …Run Code Online (Sandbox Code Playgroud) derived ×10
c++ ×6
class ×5
base ×3
java ×2
templates ×2
virtual ×2
abstraction ×1
base-class ×1
c# ×1
f# ×1
function ×1
inheritance ×1
list ×1
methods ×1
object ×1
overloading ×1
overriding ×1
pointers ×1
storage ×1