创建一个类包含有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();
}
}
编写一个控制台应用程序,完成下列功能:
- 创建一个类,用无参数的构造函数输出该类的类名。
- 添加一个重载的构造函数,带有一个String类型的参数,在此构造函数中将传递的字符串打印出来。
- 在Main方法中创建属于这个类的一个对象,不传递参数。
- 在Main方法中创建属于这个类的另一个对象,传递一个字符串“This is a string.”。
- 在Main方法中声明类型为这个类的一个具有5个对象的数组,但不要实际创建分配到数组里的对象。
- 写出运行程应该输出的结果。
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