我是一个编程新手,所以请耐心等待我.我搜索并找不到可以回答这个问题的现有主题.我写了下面的代码,它应该根据用户是将安全对象识别为股票还是债券来吐出stock.toString()或bond.toString()罐头短语.但是,我得到"安全无法解决"的编译错误.我想这是一个问题,因为安全对象的类没有在编译时定义.真的吗?如果是这样,有没有办法解决这个问题而不采用反思方法?谢谢!
public static void main(String[] args) {
double thePrice;
double theShares;
double theEarnings;
double theRate;
String securityType;
Scanner in = new Scanner(System.in);
System.out.println("Is it a stock or a bond?");
securityType = in.nextLine();
if (securityType.compareToIgnoreCase("stock") == 0) {
System.out.println("Successfully set to STOCK");
System.out.println("What are the earnings?");
theEarnings = in.nextDouble();
Stock security = new Stock();
security.setEarnings(theEarnings);
}
else if (securityType.compareToIgnoreCase("bond") == 0) {
System.out.println("Successfully set to BOND");
System.out.println("What is the rate?");
theRate = in.nextDouble();
Bond security = new Bond();
security.setRate(theRate);
}
System.out.println("What …Run Code Online (Sandbox Code Playgroud) 我有一些番石榴Functions喜欢Function<String,Set<String>>.使用那些FluentIterable.transform()导致a FluentIterable<Set<String>>,但我需要一个FluentIterable<String>.所以现在我的想法是子类化FluentIterable<E>并添加一个新方法transform2(),它只是在返回之前将所有内容合并到一个集合中.
原始转换方法如下所示:
public final <T> FluentIterable<T> transform(Function<? super E, T> function) {
return from(Iterables.transform(iterable, function));
}
Run Code Online (Sandbox Code Playgroud)
我为我的子类和transform2()方法想到了这样的东西:
public abstract class FluentIterable2<E> extends FluentIterable<E>
{
public final <T> FluentIterable<T> transform2(Function<? super E, Collection<T>> function) {
// (PROBLEM 1) Eclipse complains: The field FluentIterable<E>.iterable is not visible
Iterable<Collection<T>> iterables = Iterables.transform(iterable, function);
// (PROBLEM 2) Collection<T> merged = new Collection<T>(); // I need a container / …Run Code Online (Sandbox Code Playgroud) 我有两个子类SubClassA和SubClassB,它们都是ClassA的子类,它有一个名为someProperty的属性.我想在if语句里面决定使用哪些子类,然后才能使用这个对象.
if(conditionA){
SubClassA *myObject;
}
else{
SubClassB *myObject;
}
myObject.someProperty=someValue;
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为我不能在if-else语句之外使用myObject.我怎么解决这个问题?是否可以首先创建父对象然后将其更改为属于子类?
我遇到了以下问题:
我有一个Test带模板的课T.当我创建这个类的对象,专门研究其他常见类时,一切正常.在类的构造函数中,Test有一行代码可以创建模板类的对象;
但是当我尝试实例化这个类(Test)时,SubDummy使用抽象类(Dummy)的子类()专门化它,我得到一个错误:无法实例化一个抽象类.
示例代码(简化为本文的目的):
class Dummy
{
private:
virtual void _nothing() = 0; // this makes the class abstract
};
class SubDummy : public Dummy
{
public:
SubDummy() :
Dummy()
{
}
};
template <class T>
class Test
{
public:
Test()
{
T t; // here the compiler reports the error
}
};
int main()
{
Test<SubDummy> x;
}
Run Code Online (Sandbox Code Playgroud) 我正在学习类,当我调用类"Animal"时,它给出了错误:"Str对象不可调用"
这是我的代码:
class Animal(object):
def __init__(self, name):
self.name = name
def name(self):
print '%s' %(self.name)
rock=Animal("rock")
rock.name()
Run Code Online (Sandbox Code Playgroud) 所以我有一个类文件,里面只有我的枚举,看起来像这样
public class FactionNames {
public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, CANDOR};
}
Run Code Online (Sandbox Code Playgroud)
我有一个在构造函数中使用这些枚举的类,它看起来像这样
public Dauntless(String f, String l, int a, int ag, int end, Faction d) {
super(f, l, a, d);
if (ag >= 0 && ag <= 10) {
this.agility = ag;
} else {
this.agility = 0;
}
if (end >= 0 && end <= 10) {
this.endurance = end;
} else {
this.endurance = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,为了确保此类中的所有内容都正常工作,我想在驱动程序中创建一些 Dauntless 对象,但我不断收到这些错误
D:\Documents\Google Drive\Homework\1331
Test.java:3: error: cannot …Run Code Online (Sandbox Code Playgroud) 因此,如果我有以下 Super 类:
class Super {
public:
string member = "bla bla";
void doSth() { cout << member; }
};
Run Code Online (Sandbox Code Playgroud)
还有一个继承 Super 的 Sub 类:
class Sub : Super {
public:
string member2 = "bla bla 2";
};
Run Code Online (Sandbox Code Playgroud)
然后,当我有一个 Sub 对象时,我无法访问 Super 的成员,即使它们是公共的。
using namespace std;
int main(){
Sub sub;
cout << sub.member2 << endl;
cout << sub.member << endl; // error: public Super::member is inaccessible
sub.doSth(); // error: public Super::doSth() is inaccessible
}
Run Code Online (Sandbox Code Playgroud)
但为什么如果它们是公开的呢?或者我做错了什么?
我\xe2\x80\x99正在研究OOP,并且我\xe2\x80\x99已经得到了这篇论文。因为我知道从超类继承子类就像:class superClass : subClass{ }但我\xe2\x80\x99m 很好奇这是否是可接受的形式:class superClass : subClass1 : subclass2。
简而言之,我想从另一个类继承一个子类。因为如果我遇到一个问题,比如有一个子类,并且它是从超类继承的,因为它们具有公共数据,该怎么办?\n这样可以吗?
\n\n但这里有一个问题:如果我需要另一个子类,它与第一个子类和超类或主类都有共同的数据怎么办?
\n我有以下代码:
class Vehicle:
def __init__(self, name=None, color='Red', make='Toyota'):
self._name = name
self._color = color
self._make = make
@property
def name(self):
if self._name is None:
return 'name is none'
return self._name
@name.setter
def name(self, value):
self._name=value
def getColor(self):
return self._color
def getMake(self):
return self._make
class Car(Vehicle):
def __init__(self, mode='Running', *args, **kwargs):
super().__init__(self, *args, **kwargs)
self._mode=mode
def getMode(self):
return self._mode
Run Code Online (Sandbox Code Playgroud)
我想启动汽车的名称,但是以下都不起作用。有没有简单的方法来做到这一点?
mycar=Car('Corolla')
mycar=Car(name='Corolla')
Run Code Online (Sandbox Code Playgroud) class Foo(list):
def bar(self):
return super().__getitem__(slice(None))
def baz(self):
return super()
a = Foo([0, 1, 2, 3])
print(a.bar()) # [0, 1, 2, 3]
print(a.baz()) # <super: <class 'Foo'>, <Foo object>>
# a.bar() provides a copy of the underlying list as can be seen from the fact that each result of a.bar() has a different id
print(id(a.bar())) # id1
print(id(a.bar())) # id2 != id1
Run Code Online (Sandbox Code Playgroud)
我最近有一个用例,我需要子类化list并需要从子类 ( ) 内访问底层列表Foo。我以为super()会提供基础列表,但没有。相反,我必须super().__getitem__(slice(None))提供底层列表的副本。如何直接访问底层列表?我的理解中缺少什么super()?
非常感谢!
subclass ×10
java ×3
python ×3
c++ ×2
inheritance ×2
superclass ×2
args ×1
c# ×1
class ×1
collections ×1
enums ×1
generics ×1
guava ×1
init ×1
list ×1
mainclass ×1
objective-c ×1
polymorphism ×1
python-2.7 ×1
super ×1
templates ×1