「Java学习打卡」47、程序练习

创建一个类包含有protected数据。在相同的文件里创建第二个类,用一个方法操纵第一个类里的protected数据。

class File0 {
    protected int i=5;
}
class File1 extends File0{
     void NewMethod(){
        System.out.println(this.i);
        this.i += 10;
        System.out.println(this.i);
    }
}

public class Test {
    public static void main(String[] args){
        File1 f=new File1();
        f.NewMethod();
    }
}

编写一个控制台应用程序,完成下列功能:

  1. 创建一个类,用无参数的构造函数输出该类的类名。
  2. 添加一个重载的构造函数,带有一个String类型的参数,在此构造函数中将传递的字符串打印出来。
  3. 在Main方法中创建属于这个类的一个对象,不传递参数。
  4. 在Main方法中创建属于这个类的另一个对象,传递一个字符串“This is a string.”。
  5. 在Main方法中声明类型为这个类的一个具有5个对象的数组,但不要实际创建分配到数组里的对象。
  6. 写出运行程应该输出的结果。
class FileControl {
    FileControl(){
        System.out.println(getClass().getName());
    }
    FileControl(String str){
        System.out.println(str);
    }

}

public class Test {
    public static void main(String[] args){
        FileControl t1=new FileControl();
        FileControl t2=new FileControl("This is a string");
        FileControl[] t3=new FileControl[5];
    }
}

程序运行结果:
FileControl
This is a string

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值