码迷,mamicode.com
首页 > 编程语言 > 详细

java关键字:this,static

时间:2015-07-31 09:10:55      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:java

public class Demo2 {
public static void main(String[] args) {
// this关键字的使用
Demo1 d1=new Demo1("Tom", 13);
System.out.println(d1.getInfo());
//使用this调用构造方法
Demo3 d3=new Demo3("Cat");
System.out.println(d3.getInfo());
//this表示当前对象
//当前对象:正在调用类中方法的对象
Demo4 de1=new Demo4("Tom", 15);
Demo4 de2=new Demo4("Tom", 15);
if(de1.compare(de2)){
System.out.println("两个对象相等");
}else{
System.out.println("两个对象不相等");
}
//static关键字简单用法
StaticDemo1 sd1=new StaticDemo1("tom");
StaticDemo1 sd2=new StaticDemo1("cat");
System.out.println("---修改之前---");
sd1.info();
sd2.info();
System.out.println("---修改之后---");
sd1.city="北京";
sd1.info();
sd2.info();
System.out.println("---------------");
StaticDemo2 sde1=new StaticDemo2("tom");
StaticDemo2 sde2=new StaticDemo2("cat");
System.out.println("---修改之前---");
sde1.info();
sde2.info();
System.out.println("---修改之后---");
StaticDemo2.setCity("南京");
sde1.info();
sde2.info();
//统计一个类实例化多少对象
new StaticDemo3();
new StaticDemo3();
new StaticDemo3();
//main程序入口方法,验证参数
System.out.println("---------------");
if(args.length<3){
System.out.println("参数少于3个,退出程序");
//写一个非0的参数,表示退出程序
System.exit(1);
}
for(int i=0;i<args.length;i++){
System.out.println("第"+(i+1)+"个参数");
}
}
}
class Demo1{
private String name;
private int age;
//使用this把传递的值赋值给类中的属性
public Demo1(String name,int age){
this.name=name;
this.age=age;
}
public String getInfo(){
return name+"--"+age;
}
}
class Demo3{
private String name;
public Demo3(){
System.out.println("实例化一个新的对象");
}
public Demo3(String name){
//这里调用的是无参构造方法
this();
this.name=name;
}
public String getInfo(){
return name;
}
}
class Demo4{
private String name;
private int age;
public Demo4(String name,int age){
this.setName(name);
this.setAge(age);
}
public boolean compare(Demo4 d4){
//表示当前调用方法的对象
Demo4 de1=this;
//p2是传递到方法中的对象
Demo4 de2=d4; 
if(de1.name.equals(de2.name)&&de1.age==de2.age){
return true;
}else{
return false;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class StaticDemo1{
String name;
//使用static声明的属性
static String city="上海";
public StaticDemo1(String name){
this.name=name;
}
public void info(){
System.out.println(this.name+"--"+city);
}
}
class StaticDemo2{
String name;
//使用static声明的属性
static String city="上海";
public static void setCity(String c){
city=c;
}
public StaticDemo2(String name){
this.name=name;
}
public void info(){
System.out.println(this.name+"--"+city);
}
//使用是static声明方法
public static String getCity(){
return city;
}
}
class StaticDemo3{
public static int count=0;
public StaticDemo3(){
count++;
System.out.println("实例化"+count+"对象");
}
}

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

java关键字:this,static

标签:java

原文地址:http://blog.csdn.net/dzy21/article/details/47164457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!