美文网首页
this关键字---20161121

this关键字---20161121

作者: 海豚的小小海 | 来源:发表于2016-11-21 22:35 被阅读0次

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](https://img.haomeiwen.com/i2562717/4a57d12e1e2c92c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

  • this关键字---20161121

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

  • JavaScript 原型链

    请移步:https://blog.cdswyda.com/post/20161121

  • 20161121

  • 20161121😊

    现在哈尔滨已经零下二十一度,哈哈哈,已经穿上了最厚的棉裤啦,穿着面包鞋还有大棉袄哈哈,戴着帽子口罩,这样就很暖和了。

  • 20161121

    今天下了个Airbnb 以前在微博上看到的那些美好的住宅通过它好像都可以实现去居住一下的体验 佛罗伦萨的树屋啦 带...

  • 20161121

    从噩梦中惊醒,拿起手机一看才2点了。最近总是半夜惊醒,然后反思自己,觉得还是自己不够努力,才会这样!说得漂亮,做的...

  • 20161121

    CommonCrypto加密解密参数说明 参数说明 CCOperation op加密kCCEncrypt还是解密k...

  • 小马20161121

    今天雨夹雪,好冷啊! 感恩这个天气让孩子不能去幼儿园,因为昨晚睡得太晚,早上我起不来了,不去幼儿园的宝宝,在床上醒...

  • 20161121作业

    主题词:时间,吃饭,睡觉 你现在拥有的最值钱的是什么?不是钱财,不是权利,而是你的时间。从生下来到去世,去掉不能...

  • 20161121《宝贝》

    今天外婆去看你了,奶奶说一看到外婆,就要外婆抱,没忘记外婆呢。真是好样的,外婆爱你哟 拿着手机,你总是把手机翻到背...

网友评论

      本文标题:this关键字---20161121

      本文链接:https://www.haomeiwen.com/subject/ooxppttx.html