首页 > 代码库 > this关键字的构造方法的使用

this关键字的构造方法的使用

 1 package com.wh.Object3; 2  3 public class this_Demo { 4     private String name; 5     private double price; 6     private int num; 7  8     public String getName() { 9         return name;10     }11 12     public void setName(String name) {13         this.name = name;14     }15 16     public double getPrice() {17         return price;18     }19 20     public void setPrice(double price) {21         this.price = price;22     }23 24     public int getNum() {25         return num;26     }27 28     public void setNum(int num) {29         this.num = num;30     }31 32     public this_Demo() {33         //this关键字的构造方法的使用 34         this("可乐", 2.5, 1001);35     }36 37     public this_Demo(String name, double price, int num) {38         this.name = name;39         this.price = price;40         this.num = num;41     }42 43     public static void main(String[] args) {44         /*45          * this("","",""); 代表一个构造方法46          */47         this_Demo th=new this_Demo();48         System.out.println(th.getName());49         System.out.println(th.getPrice());50         System.out.println(th.getNum());51     }52 }

 

this关键字的构造方法的使用