当前位置: 技术问答>java相关
小程序段,帮忙查错,谢谢
来源: 互联网 发布时间:2017-04-22
本文导语: import java.lang.String; interface Calculate{ public int i = 4; public int j = 4; public void cal(); } class CalAdd implements Calculate{ int i,j; void cal(){ System.out.println(i+j); } } class CalDec implements Calculate{ int i,j; void cal(){ Syst...
import java.lang.String;
interface Calculate{
public int i = 4;
public int j = 4;
public void cal();
}
class CalAdd implements Calculate{
int i,j;
void cal(){
System.out.println(i+j);
}
}
class CalDec implements Calculate{
int i,j;
void cal(){
System.out.println(i-j);
}
}
public class f3{
public static void main(String[] args){
Calculate c[] = {new CalAdd(),new CalDec()};
c[0].cal();
c[1].cal();
}
}
interface Calculate{
public int i = 4;
public int j = 4;
public void cal();
}
class CalAdd implements Calculate{
int i,j;
void cal(){
System.out.println(i+j);
}
}
class CalDec implements Calculate{
int i,j;
void cal(){
System.out.println(i-j);
}
}
public class f3{
public static void main(String[] args){
Calculate c[] = {new CalAdd(),new CalDec()};
c[0].cal();
c[1].cal();
}
}
|
给类CalAdd和CalDec中的变量和方法前面加上public
|
你在CalAdd和CalDec不对i,j进行覆盖就可以了啊..............
你继承了Calculate接口,直接可以用它里面定义的变量了啊.........
你继承了Calculate接口,直接可以用它里面定义的变量了啊.........
|
import java.lang.String;
interface Calculate{
public int i = 4;
public int j = 4;
public void cal();
}
class CalAdd implements Calculate{
// public int i,j;
public void cal(){
System.out.println(i+j);
}
}
class CalDec implements Calculate{
//public int i,j;
public void cal(){
System.out.println(i-j);
}
}
public class f3{
public static void main(String[] args){
Calculate c[] = {new CalAdd(),new CalDec()};
c[0].cal();
c[1].cal();
}
}
interface Calculate{
public int i = 4;
public int j = 4;
public void cal();
}
class CalAdd implements Calculate{
// public int i,j;
public void cal(){
System.out.println(i+j);
}
}
class CalDec implements Calculate{
//public int i,j;
public void cal(){
System.out.println(i-j);
}
}
public class f3{
public static void main(String[] args){
Calculate c[] = {new CalAdd(),new CalDec()};
c[0].cal();
c[1].cal();
}
}
|
Interface中的
public int i = 4;
public int j = 4;
实际上缺省为:
static final public int i = 4;
static final public int j = 4;
若要想动态改变,只能用抽象类了
public int i = 4;
public int j = 4;
实际上缺省为:
static final public int i = 4;
static final public int j = 4;
若要想动态改变,只能用抽象类了