有几种对象类型,如:classAa a和classAa a=new classAa()有什么区别,可以new 一个接口对象吗,该如何调用方法??

grant999 2005-12-15 05:42:30
??
...全文
141 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
grant999 2005-12-16
  • 打赏
  • 举报
回复
import java.text.*;
import java.util.*;
import java.io.*;

interface ghy_interface {
String show_yourname(String yourname);
}

class ghy_class implements ghy_interface {
public String show_yourname(String yourname) {
return this.yourname.toUpperCase();
}

}

public class ww(){
ghy_interface q;
static ww w;
public static ghy_interface qq(){

return w.q;
}
}



public class ghytest {




public static void main(String[] args) {

System.out.println(ww.qq().show_yourname("ghyghost"));
}

}
看看这个例子,报错是ghytest.java:16: 需要 '{'
public class ww()
^
ghytest.java:37: 需要 '}'
}
这是为什么???????????????????
  • 打赏
  • 举报
回复
import java.text.*;
import java.util.*;
import java.io.*;

interface ghy_interface {
String show_yourname(String yourname);
}

class ghy_class implements ghy_interface {
public String show_yourname(String yourname) {
return yourname.toUpperCase();
}

}

public class ghytest {

public static void main(String[] args) {
ghy_class ghy=new ghy_class();
System.out.println(ghy.show_yourname("ghyghost"));
}

}

一个最简单的接口例子,不知道满足没有满足楼主的需要
  • 打赏
  • 举报
回复
调用接口的方法你得先用类实现接口,然后再调用类实例中的方法。
CalvinKyle 2005-12-15
  • 打赏
  • 举报
回复
可以new 一个接口对象吗
============================不可
  • 打赏
  • 举报
回复
对象的引用永远是在栈上,而对象永远在堆中。
crazycy 2005-12-15
  • 打赏
  • 举报
回复
classAa a
======
声明一个变量,占用了栈内存,但是此时没有生成对象

classAa a=new classAa()
======
这个时候生成了一个对象,在堆内存中,并且把栈内存中的a指向了这个堆对象

java中内存分为堆内存和栈内存,还有放置其他的内存

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧