3. python 和java的区别

1、python 和java 继承的区别

class Person():
    def __init__():
      print("person")
		
class Student():
    def __init__():
      print("Student")
		 
a=Student():
# 当实例化Student这个类的时候, 会首先调用自己的init 方法,当自己没有init方法是才会调用父类的init 方法
package com.makuo.day3;

public class Exercise2 {

    public static void main(String[] args) {


        Exercise3 e3=new Exercise3();
	//Exercise5和Exercise3都有自己的构造器,Exercise3 继承Exercise5 ,那么 Exercise5的Exercise3的构造器都会被调用

    }
}

class  Exercise3 extends Exercise5{

    public Exercise3(){
//        super();    //super()方法会自动添加

        System.out.println("Exercise3");
    }
}

class Exercise5 {

    public Exercise5() {
        System.out.println("Exercise5");
    }
}

python的子类如果自己有初始化方法不会使用父类的,java默认调用父类的构造器

问题:

package com.makuo.day3;

public class Test1 {

    public static void main(String[] args) {

        B b1=new B();
    }
}



class  A{
    A(){
        System.out.println("a");
    }
    A(String name){
        System.out.println("a name");
    }
}

class B extends A{
    B(){
        this("abc");  // 这里是this,不会调用父类的构造器方法
        System.out.println("B");
    }
    B(String name){
//        super();  这里程序会自动调用父类的构造器
        System.out.println("B name");
    }
}

// a
// B name
// B
posted @ 2022-09-17 00:03  mk-备忘  阅读(35)  评论(0)    收藏  举报