this关键字---20161121

this关键字:
1、总是指向当前对象
2、在一个构造方法中调用另一个构造方法,必须是构造函数的第一条语句

this 总是指向当前对象的引用

public class Product {
    private String productName;
    private int quantity;
    private double price;
    private double totalPrice;

    public double computerTotalPrice() {
        return price * quantity;}
    public void show() {
        System.out.println("\t" + "产品:" + productName);
        System.out.println("\t" + "数量:" + quantity);
        System.out.println("\t" + "价格:" + price);
        System.out.println("\t" + "总价:" + this.computerTotalPrice());
        System.out.println("**************************");}
    public Product() {}
    public Product(String productName, int quantity, double price) {
        this.price = price;
        this.productName = productName;
        this.quantity = quantity;
    }

        //set get函数,用来调用私有化变量
    public void setproductName(String productName) {
        this.productName = productName;}
    public String getproductName() {
        return productName;}
    public void setQuantity(int quantity) {
        this.quantity = quantity;}
    public int getQuantity() {
        return quantity;}
    public void setPrice(double price) {
        this.price = price;}
    public double getPrice() {
        return price;}
    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;}
    public double getTotalPrice() {
        return totalPrice;}
}```
对象数组:
```javaStudent stud = new Student[ 3 ];```  //数组中只能存放Student对象或者子类对象
```java
public class Exend1 {
    public static void main(String[] agrs) {
        System.out.println("产品订单" + "-------------------");
        Product[] pro = new Product[3];
        pro[0] = new Product("蓝球", 5, 80.0);
        pro[1] = new Product("VCD带", 6, 25.0);
        pro[2] = new Product("钢笔", 3, 12.0);
        for (int i = 0; i < pro.length; i++) {
            pro[i].show();
        }
        // for( Product i : pro ) {
        // i.show();
        // }
        System.out.println( "\n" );
    }
}```

对象数组在内存中的存放:
![对象数组存储图.png](http://upload-images.jianshu.io/upload_images/2562717-4a57d12e1e2c92c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(this关键字---20161121)