首页 > 其他 > 详细

对像的创建及使用

时间:2015-03-21 15:22:05      阅读:225      评论:0      收藏:0      [点我收藏+]

对象的创建

类名   对象名称 =      null;               //   声明对象

对象名称    =new    类名();         //实例化对象    

 以上格式产生对象分为声明对象和实例化对象两步 。

   当然也可以一步直接实现。

类名    对象名称   =new   类名();

  了解格式后请看具体代码

class Person{
	String name;
	int age;
	public void tell(){
		System.out.println("名字:"+name+",年龄:"+age);
	}
}
public class DataDemo04 {
	public static void main(String args[]){
		Person per1=null;
		Person per2=null;
		per1=new Person();
		per2=new Person();
		per1.name="大表哥";
		per2.name="张三";
		per1.age=38;
		per2.age=48;
		per1.tell();
		per2.tell();
	}
	 

}

  程序运行结果

名字:大表哥,年龄:38
名字:张三,年龄:48

  

对像的创建及使用

原文:http://www.cnblogs.com/dung/p/4355584.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!