• this的二种使用方式


    package com.ht.TestThis;
    
    public class TestThisKey {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Student s1 = new Student("ht",23,"男",107);
    		System.out.println(s1.name+"\t"+s1.age+"\t"+s1.sex+"\t"+s1.score);
    	}
    
    }
    class Student{
    	String name;
    	int age;
    	String sex;
    	double score;
    	public Student() {};
    	public Student(String n,int a,String s,double d) {
    		System.out.println("四参构造执行");
    		name = n;
    		age = a;
    		sex = s;
    		score = d;
    	}
    }
    

      

    package com.ht.TestThis;
    
    public class TestThisKey {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Student s1 = new Student("ht",23,"男",107);
    		Student s2 = new Student("hts",223,"男s",1207);
    		System.out.println(s1.name+"\t"+s1.age+"\t"+s1.sex+"\t"+s1.score);
    		System.out.println(s2.name+"\t"+s2.age+"\t"+s2.sex+"\t"+s2.score);
    	}
    
    }
    class Student{
    	String name;
    	int age;
    	String sex;
    	double score;
    	public Student() {};
    	public Student(String name,int age,String sex,double score) {
    		System.out.println("四参构造执行");
    		this.name = name;
    		this.age = age;
    		this.sex = sex;
    		this.score = score;
    	}
    }
    

      this第二种用法:调用本类中的其他构造方法。如:this(),this(实参)

    package com.ht.TestThis;
    
    public class TestThisKey {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Student s1 = new Student("ht",23,"男",107);
    		Student s2 = new Student("hts",223,"男s",1207);
    		System.out.println(s1.name+"\t"+s1.age+"\t"+s1.sex+"\t"+s1.score);
    		System.out.println(s2.name+"\t"+s2.age+"\t"+s2.sex+"\t"+s2.score);
    	}
    
    }
    class Student{
    	String name;
    	int age;
    	String sex;
    	double score;
    	public Student() {};
            public Student(String name,int age,String sex) {
                  this.name= name;
    	      this.age= age;
                  this.sex= sex;
    	}
    
    	public Student(String name,int age,String sex,double score) {
                    this(name,age,sex)//this[实参]必须在构造方法的首行 只能在构造方法,不能在普通方法中
    		this.score = score;
    	}
    }
    

      

  • 相关阅读:
    前端工程化浅学
    jQuery学习
    黄金票据和白银票据获取域控权限
    [转]0day零距离
    [转]走近0day
    [转]人生如解 -- 关于破解组织的乱弹
    [转]WAREZ无形帝国
    [转]BSD系统正在死亡?一些安全研究人员这样认为
    Solaris:你好奇的十件事
    Windows和Office激活汇总
  • 原文地址:https://www.cnblogs.com/ht955/p/16297846.html
Copyright © 2020-2023  润新知