这是我的完整代码
package user;
public class User {
private String userID,emailAddress;
private int numOfAccesses;
public static int counter=0;
public User(String iD,String emailA,int nOA){
userID=iD;
emailA=emailAddress;
nOA=numOfAccesses;
counter++
}
public User(String aID,String aEmail){
this(aID,aEmail,0);
}
public User(){
this(null,null,0);
}
public String getInfos(){
String message =("user ID "+userID+"\n"+"email address is "+emailAddress+"\n"+"number of accesses "+numOfAccesses);
return message;
}
public void setInfos(String setID,String setEmail){
setID=userID;
setEmail=emailAddress;
}
public int getCounter(){
return counter;
}
public void updateNumOfAccesses(){
numOfAccesses++;
}
public class testUser extends User{
User user1=new User();
System.out.println("counter is "+user1.getCounter);
}
public static void main(String[] args) {
}
}
我很抱歉我是java中的菜鸟,但我真的很困惑,为什么我能调用getCounter方法,而我明确做对了。是因为继承?或者我没有使用柜台吗?请帮忙
答案 0 :(得分:0)
您有语法错误
你想要
System.out.println("counter is "+user1.getCounter ());
正在调用方法
同时更改此代码
public class testUser extends User{
User user1=new User();
System.out.println("counter is "+user1.getCounter);
}
public static void main(String[] args) {
}
到
public static void main(String[] args) {
User user1=new User();
System.out.println("counter is "+user1.getCounter ());
}
另一个错误是
counter++ is not terminated with a `;`
答案 1 :(得分:0)
public class User {
...
class TestUser extends User {
User user1 = new User();
public void someMethodName() {
System.out.println("counter is " + user1.getCounter());
}
}
public static void main(String[] args) {
User u1 = new User();
TestUser tu1 = u1.new TestUser();
tu1.someMethodName();
}
}
输出:
counter is 3
正在调用User()三次创建。
extends User
时,它会调用User
类的构造函数来递增计数器。User user1 = new User();
再次递增计数器