1.
class Base{
public Bsae(){}//删除 正常运行
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);//改为new MyOver() 正常运行
}
MyOver(int i){
super(i);
}// 不能少,去掉则出错}正常运行
2.
class Base{
public Bsae(){}//删除 并改为new MyOver() 时 出错,找不到构造函数。
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);
}
}3.
class Base{
public Base(){}
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver("",7);
}
MyOver(int i){System.out.println("MyOver constructor") ;
}
MyOver(String s, int i){
this(i);//////关键
}}输出:MyOver constructor
4.class Base{
public Base(){}
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(7);
}
MyOver(int i){ //看作是对父类默认构造函数的重写System.out.println("MyOver constructor") ;
}
}输出:MyOver constructor5.class Base{
public Base(){}}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(7);
}
MyOver(int i){System.out.println("MyOver constructor") ;
}
}输出:MyOver constructorBased on the knowledge that constructors are not inherited,
it must be obvious that overriding is irrelevant.
If you have a class called Base and you create a child that extends it,
for the extending class to be overriding the constructor it must have the same name.
This would cause a compile time error. Here is an example of this nonsense hierarchy.
class Base{
public Bsae(){}//删除 正常运行
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);//改为new MyOver() 正常运行
}
MyOver(int i){
super(i);
}// 不能少,去掉则出错}正常运行
2.
class Base{
public Bsae(){}//删除 并改为new MyOver() 时 出错,找不到构造函数。
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);
}
}3.
class Base{
public Base(){}
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver("",7);
}
MyOver(int i){System.out.println("MyOver constructor") ;
}
MyOver(String s, int i){
this(i);//////关键
}}输出:MyOver constructor
4.class Base{
public Base(){}
public Base(int i){}
}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(7);
}
MyOver(int i){ //看作是对父类默认构造函数的重写System.out.println("MyOver constructor") ;
}
}输出:MyOver constructor5.class Base{
public Base(){}}public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(7);
}
MyOver(int i){System.out.println("MyOver constructor") ;
}
}输出:MyOver constructorBased on the knowledge that constructors are not inherited,
it must be obvious that overriding is irrelevant.
If you have a class called Base and you create a child that extends it,
for the extending class to be overriding the constructor it must have the same name.
This would cause a compile time error. Here is an example of this nonsense hierarchy.
class Base{
public Bsae(){}//删除 正常运行 //错了.....
public Base(int i){}
} public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);//改为new MyOver() 正常运行
}
MyOver(int i){
super(i);
}// 不能少,去掉则出错 }
//1.
public class Base {
public Base() { // 你这里写错了
}// 删除 正常运行 public Base(int i) {
}
}class MyOver extends Base { // 这里不用 public 修饰
public static void main(String arg[]) {
MyOver m = new MyOver(10);// 改为new MyOver() 正常运行
} MyOver(int i) {
super(i);
}// 不能少,去掉则出错}// 正常运行
// 2.
public class Base {
public Base() { //还是这里写错了
}// 删除 并改为new MyOver() 时 出错,找不到构造函数。 public Base(int i) {
}
}class MyOver extends Base { //还是同上
public static void main(String arg[]) {
// MyOver m = new MyOver(10); 没有参数的构造函数不能这样。
MyOver m = new MyOver();
}
}
// 3. 你都是同样的问题
public class Base {
public Base() {
} public Base(int i) {
}
}class MyOver extends Base {
public static void main(String arg[]) {
MyOver m = new MyOver("", 7);
} MyOver(int i) { System.out.println("MyOver constructor");
} MyOver(String s, int i) {
this(i);// ////关键
}
// 输出:MyOver constructor
}
// 4. 同上面问题public class Base {
public Base() {
} public Base(int i) {
}
}class MyOver extends Base {
public static void main(String arg[]) {
MyOver m = new MyOver(7);
} MyOver(int i) { // 看作是对父类默认构造函数的重写 System.out.println("MyOver constructor");
}}
// 输出:MyOver constructor// 5. 同上問題public class Base {
public Base() {
} } class MyOver extends Base {
public static void main(String arg[]) {
MyOver m = new MyOver(7);
} MyOver(int i) { System.out.println("MyOver constructor");
} } // 输出:MyOver constructor