Java笔记_this关键字_HomeWork(9 - 题)

第九题

/**
 * @ClassName HomeWork09
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/26 16:20
 * @Version 1.0
 **/
  
/*
Notes:
    定义Music类,里面有音乐名name,音乐时长times属性,
    并有播放play功能和返回本身属性信息的功能方法getInfo.
    Define the 'Music' class, which has the music name 'name',
    the music duration and 'times' attributes, and has the
    'play' function and the function method 'getInfo' to return
    its own attribute information.
  */

public class HomeWork09 {
    public static void main(String[] args) {
        A09 a = new A09("峰哥之歌", 135);
        a.play();
        a.getInfo();
    }
}

class A09 {
    String name;
    int times;
    public A09(String name, int times) {
        this.name = name;
        this.times = times;
    }

    public void play() {
        System.out.println("====Start playing====");
    }

    public void getInfo() {
        System.out.println("Song name: " + this.name + "\nSong duration:" + this.times + "秒");
    }
}

/*
Program running results:
------------------------------------
====Start playing====
Song name: 峰哥之歌
Song duration:135秒
------------------------------------
*/

第十题(还需分析)

/**
 * @ClassName HomeWork10
 * @Description TODO
 * @Author Orange
 * @Date 2021/4/26 17:01
 * @Version 1.0
 **/

/*
Notes:
    试写出以下代码的运行结果
    class Demo{
        int i,j = 100;
        public void m () {
            int j = i++;
            System.out.println("i = " + i);
            System.out.println("j = " + j);
        }
    }
    class Test {
        public static void main(String[] args) {
            Demo d1 = new Demo();
            Demo d2 = d1;
            d2.m();
            System.out.println(d1.i);
            System.out.println(d2.i);
        }
    }
  */

public class HomeWork10 {
    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = d1;
        d2.m();
        System.out.println(d1.i);
        System.out.println(d2.i);
    }
}

class Demo{
    int i = 100;
    public void m () {
        int j = i++;
        System.out.println("i = " + i);
        System.out.println("j = " + j);
    }
}


/*
Program running results:
------------------------------------
i = 101
j = 100
101
101
------------------------------------
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OrangeQG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值