我配合楼上的给你弄个例子吧,以我的理解,研究研究,一起学习.
public class Test //定义一个类Test
{
public Test() //一个构造器,也叫构造函数.它的名字必须
//和类名一样
{
System.out.println("This is the test.");
} public void methodone() //定义一个方法
{
System.out.println("This is the method.");
}
}public class Model
{
public static void main(String[] args)
{
Test test = new Text(); //利用new调用构造器Text()
test.methodone(); //对象test调用methodone()就是实例的应用
//它必须通过对象调用,不能用类
}
}这个程序的输出结果应该是:
This is the test.
This is the method.最后的问题我也不懂,关注!
public class Test //定义一个类Test
{
public Test() //一个构造器,也叫构造函数.它的名字必须
//和类名一样
{
System.out.println("This is the test.");
} public void methodone() //定义一个方法
{
System.out.println("This is the method.");
}
}public class Model
{
public static void main(String[] args)
{
Test test = new Text(); //利用new调用构造器Text()
test.methodone(); //对象test调用methodone()就是实例的应用
//它必须通过对象调用,不能用类
}
}这个程序的输出结果应该是:
This is the test.
This is the method.最后的问题我也不懂,关注!
我想问一下的是,构造器都是在类中定义的么?还有,我对上述内容的理解是:我能说在类Test中的构造器的原始方法是输出This is the test,而建立在这个类中的方法methodone通过另一个类Model
中调用Test未调用的方法methodone么?而且对于对象我还有个问题:
例如:
String greeting = "hello";
String s = greeting.substring(0,4);
中的greeting 是为什么可以调用String中的方法substring,难道greeting也是个对象么?我对greeting的理解是它是个String类型的变量,对于这点,我不大懂,希望各位继续讲解一下。谢谢!
================================================================CSDN 论坛助手 Ver 1.0 B0402提供下载。 改进了很多,功能完备!★ 浏览帖子速度极快![建议系统使用ie5.5以上]。 ★ 多种帖子实现界面。
★ 保存帖子到本地[html格式]★ 监视您关注帖子的回复更新。
★ 可以直接发贴、回复帖子★ 采用XML接口,可以一次性显示4页帖子,同时支持自定义每次显示帖子数量。可以浏览历史记录!
★ 支持在线检测程序升级情况,可及时获得程序更新的信息。★★ 签名 ●
可以在您的每个帖子的后面自动加上一个自己设计的签名哟。Http://www.ChinaOK.net/csdn/csdn.zip
Http://www.ChinaOK.net/csdn/csdn.rar
Http://www.ChinaOK.net/csdn/csdn.exe [自解压]
无论你在哪调用methodone(),调用它的对象必须是Test类型,也就是说在调用这个方法前,必须定义那个对象为Test,如:Test aaa = new Test();
在这个例子里,只要调用了构造器,就会执行构造器内的语句,不知道你会不会VB,在VB内,有个FORM_LAOD,只要打开这个FORM,就执行这里的语句,就是这个意思.另外,方法methodone()在哪都可以调用,在Test内也是一样可以调用.