码迷,mamicode.com
首页 > 其他好文 > 详细

对像的创建及使用

时间:2015-03-21 15:24:43      阅读:119      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!