我想在属性中添加属性作为接口契约的一部分.我读到这是不可能的,因为它确实为类增加了行为,这是一个没有使用/制作接口的东西.但如果是这样的话,为什么它会编译而不是发出错误,如果它根本没有效果或者我错过了什么?
public class MyAttr : Attribute { }
public interface MyInterface
{
[MyAttr]
string Foo { get; set; }
}
public class MyClass : MyInterface
{
public string Foo { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我有下面的代码,我使用类"B"继承类"A",而我希望从接口IMy实现F函数.但是编译器告诉我我正在隐藏接口方法"F".所以运行结果是"A".
我希望这个程序输出"B".我不希望使用隐式接口实现,因为我希望在main函数中使用正常的多态性.
如何更正我的代码?谢谢.
public interface IMy
{
void F();
}
public class A : IMy
{
public void F()
{
Console.WriteLine("A");
}
}
public class B : A
{
public void F()
{
Console.WriteLine("B");
}
}
class Program
{
static void Main(string[] args)
{
IMy my = new B();
my.F();
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下界面:
interface IMyInterface {}
Run Code Online (Sandbox Code Playgroud)
以下两个实现:
class MyImplementation : IMyInterface {}
class MyOtherImplementation : IMyInterface {}
Run Code Online (Sandbox Code Playgroud)
鉴于此,以下编译:
IMyInterface ReturnImplementation()
{
if(condition)
{
return new MyImplementation();
}
else
{
return new MyOtherImplementation();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这并不:
IMyInterface ReturnImplementation()
{
return condition ? new MyImplementation() : new MyOtherImplementation();
}
Run Code Online (Sandbox Code Playgroud)
为什么?假设它应该编译,我误解了什么?它是否简单,因为速记if指定了从中选择完全相同的类型?如果是这样,为什么?为什么这样受限制?
我有一个名为ReportWriter的接口,它将报告写入OutputStream,Report类包含reportRow列表,也是一个抽象类:
public interface ReportWriter<T extends ReportRow> {
OutputStream writeReport(Report<T> report) throws ReportWriterException;
}
public abstract class Report<T extends ReportRow> {
private List<T> rows = Lists.newArrayList();
...
}
public abstract class ReportRow {...}
Run Code Online (Sandbox Code Playgroud)
现在我有CsvWriter实现ReportWriter,
public class CsvWriter<T extends ReportRow> implements ReportWriter {
@Override
public ByteArrayOutputStream writeReport(Report report) throws ReportWriterException {
...
for (T row : report.getRows()) { <-- incompatible type
..write here..
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,它抱怨不兼容的类型:
require Object found T.
Run Code Online (Sandbox Code Playgroud)
我不理解在Report类中我已经指定T是ReportRow的子类,为什么我抱怨这个?
然后我尝试更新CsvWriter的writeReport,如下所示:
public class CsvWriter<T extends ReportRow> implements ReportWriter {
@Override
public …Run Code Online (Sandbox Code Playgroud) 假设我有这个界面:
public interface i{
void setImage(Image image);
}
Run Code Online (Sandbox Code Playgroud)
我希望实现该接口方法的类方法不仅可以接受作为方法参数的图像,还可以接受扩展Image的所有类.
我在界面中需要的是:
<T extends Image> void setImage(T image);
Run Code Online (Sandbox Code Playgroud)
但是,当然,这不是正确的方法.我应该如何在接口中编写通用方法?
我试图在基类上设置一个值,但我收到错误:
无法隐式转换
'PersonService'为'IMyInterface<IEntity>'.存在显式转换(您是否错过了演员?)
当我尝试创建PersonService()它时它确实实现了IMyInterface<Person>.人实施,IEntity所以我无法弄清楚为什么它不工作.
public interface IEntity { }
public class Person : IEntity { }
public interface IMyInterface<T> where T : IEntity { }
public class PersonService : IMyInterface<Person> { }
public abstract class Model : IEntity
{
public IMyInterface<IEntity> DataService { get; set; }
}
public class Test : Model
{
public Person Person { get; set; }
public Test() : base()
{
DataService = new PersonService();
}
}
Run Code Online (Sandbox Code Playgroud) 我的印象是可以实例化一个接口:
interface TestA {
String toString();
}
public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() {
return "test";
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这打印"test".为什么?该类未实现该接口.
它还创建了一个使用它的实例,new TestA()但我读取的接口无法实例化.
有人可以解释为什么它打印成功?
我遇到了一个非常棘手的情况,即实现具有与Member.below相同的方法名称的多个接口是该问题的示例代码.
public interface IBase1
{
void show();
}
public interface IBase2
{
void show();
}
public class BaseClass :IBase1, IBase2
{
void IBase1.show()
{
Console.WriteLine("IBase1.show()");
}
void IBase2.show()
{
Console.WriteLine("IBase2.show()");
}
}
Run Code Online (Sandbox Code Playgroud)
主类
class Program
{
static void Main(string[] args)
{
BaseClass obj = new BaseClass();
//How to access IBase1.show() and IBase2.show()
}
}
Run Code Online (Sandbox Code Playgroud)
这里我有两个接口,每个接口都有一个show()方法,一个类实现了这两个接口,现在我的问题是如何BaseClass在Main函数中访问这个方法.
我有问题解决是否作为函数的接口传递的对象没有初始化字段,像被定义为刚才的对象someObject{}是空的,因为所有字段,值为0或nil
如果我传递不同的对象,问题会变得更复杂,因为每个对象都有不同的类型字段值,所以在这一刻我找不到通用的方法.
例
func main(){
oo := objectOne{}
ot := objectTwo{}
oth := objectThree{"blah" , "balbal" , "blaal"}
resolveIsNotIntialized(oo)
resolveIsNotIntialized(ot)
resolveIsNotIntialized(oth)
}
func resolveIsNotIntialized(v interface{}) bool{
// and below, how resolve that oo and ot is empty
if (v.SomeMethodWhichCanResolveThatAllFiledIsNotIntialized){
return true
}
return false
}
Run Code Online (Sandbox Code Playgroud)
我想避免使用switch如下所示的用法语句,并为每个对象添加额外的功能,如果可能的话.
func unsmartMethod(v interface{}) bool{
switch v.(type){
case objectOne:
if v == (objectOne{}) {
return true
}
// and next object, and next....
}
return false
}
Run Code Online (Sandbox Code Playgroud) 我试图理解Java中的枚举,但目前找不到解释我问的问题的帖子.
我有一个Person界面和一个PersonEO类.Person接口的两种方法是setGender和getGender.我想把性别作为一个枚举.
我是否在界面中创建枚举并将其转移到课程中,或者在课堂上创建枚举?
这是我一直在尝试的但是无法让enum正确地传递给类,并且不能真正理解它将如何流经程序.
public interface Person{
public enum gender{
Female,
Male,
Unknown;
private String gender;
public String getGender(){
}
void setGender(){
}
}
------------------------------------------------------
public class PersonEO implements Person {
//Both override methods are not seen as methods from the interface
@Override
public String getGender() {
return gender;
}
@Override
public void setGender() {
//Not sure how to set the gender from the enum
this.gender = gender;
}
}
Run Code Online (Sandbox Code Playgroud)
请举例说明.
interface ×10
c# ×5
java ×4
generics ×3
.net ×2
attributes ×1
class ×1
enums ×1
go ×1
hide ×1
if-statement ×1
inheritance ×1
methods ×1
object ×1
types ×1