如何通过evrey类在接口中更改变量的值在java中实现这个接口? [英] How I can to change the value of variable in interface by evrey class implement this interface in java ?

查看:100
本文介绍了如何通过evrey类在接口中更改变量的值在java中实现这个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有界面:

I have interface :

public interface Myglobal 
{
   public int Type =0;
}



然后我有类实现我的界面如:


Then I have class implement My interface like :

Public class A implements Myglobal 
{
   Public class A ()
   {
      this.type=1; // here error because type final in interface
   }
}



我想为evrey类实现接口来改变变量类型的值。 ..所以我怎么能用java做到这一点?



我尝试了什么:



我读了java不允许......但我能做些什么让evrey类实现这个接口来改变接口中变量的值?


I want for evrey class implement the interface to change the value of variable type ... so how I can do it with java ?

What I have tried:

I read the java not allow ... but what I can to do to make evrey class implement this interface to change the value of variable in interface ?

推荐答案

请参阅第9章界面 [ ^ ]。


您已经得出结论
Type

是一个无法更改的常量。因此,你必须创建一个接口方法,然后在每个实现类中实现这个,如下所示:



is a constant that cannot be changed. Thus you have to create a interface method and then simply implement this in each implementing class like this:

public interface Myglobal 
{
     public int getType();
}
 
public class A implements Myglobal 
{
     public int getType() { return 1; }
}


这篇关于如何通过evrey类在接口中更改变量的值在java中实现这个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆