ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java与python类对比

2020-03-17 10:43:55  阅读:129  来源: 互联网

标签:调用 java 成员 python self print 父类 对比 属性


1. 构造器

在这里插入图片描述

方法和变量

在这里插入图片描述

super、self、this

在这里插入图片描述

4. python代码实例

class Test():
    # 类属性
    country = '中国'
    province = '陕西省'
    city = '西安市'
    readme = '我是父类类属性'

    
    # 成员属性
    def __init__(self, name, age, sex, district, native, party_member):
        self.name = name  # 使用(self.变量名)定义或调用成员属性
        self.age = age
        self.sex = sex
        Test.add_information = '在构造器内定义类属性'
        self.city = self.city + district  # python先执行等号后面的,等号后面的self.city为调用类属性(self.city首先会从成员属性中找有没有city,没有再从类中找。
        # 由于等号右边先于等号执行,此时成员属性还没有city,故等号右边的self.city是类属性)
        # 等号左边的self.city为成员属性,相当于定义了一个成员属性self.city,将其赋值为类中的city加上district
        # 等号右边出现self.属性或者self.方法,会先从成员属性或方法中寻找(调用),如果成员没有,就去类中找
        Test.province  = self.province + '('+ native + ')' # 等号左右两边都是类属性,该代码修改了类属性的值
        self.readme = Test.description() 
        self.laud = self.praise() 
        self.income = self.salary(party_member) # 调用成员方法
    
    # 成员方法
    def salary(self, party_member):
        if party_member:
            money = 10000
        else:
            money = 11000
        return money
    
    # 成员方法
    def more_information(self, party_member):
        less_info = self.description() #  成员方法可以调用类方法
        more_info = self.country + Test.province + self.city  # 成员方法可以调用类属性及成员属性
        money = self.salary(party_member)  # 成员方法可以调用成员方法
        print(less_info)
        print(more_info) 
        print('我的工资是' + str(money))
        self.test()
        
    def class_method_order():
        self.test()  # 首先从类方法里面找,没有再从成员方法中找
        
    def test(self,):
        print('测试调用顺序:我是父类成员方法')        
        
    def test_1(self, ):
        print('我是父类成员方法')
     
    # 类方法
    @classmethod
    def description(cls, ):
        sent = '我来自' + Test.country + Test.province + Test.city # 类方法可以调用类属性,也可以调用类方法,不再举例
        # sent = '我来自' + self.country + Test.province + Test.city  # 错误,类方法不能调用成员属性,由于self.开头,首先会从成员属性找
        # 所以即使country是类属性,但由于前面是self修饰,就会先从成员属性寻找,而类方法又不能调用成员属性,故报错
        return sent

    # 类方法
    @classmethod
    def praise(cls, ):
        sent = '作为一名' + Test.country + '人我很自豪'
        return sent
    
    # 类方法
    @classmethod
    def test_2(cls, ):
        print('我是父类类方法')
               
    @classmethod
    def test(cls,):
        print('测试调用顺序:我是父类类方法')
person = Test(name='张丽', age=30, sex='女', district='未央区', native='外来人口', party_member=True)
print(person.city) # 实例的city
print(Test.city) # 类的city,与实例city结果不同
print(Test.add_information)
print(person.add_information)
print(person.country)  # 先从实例找是否有country的属性,没找到,从类里面找,找到了,故这里是类属性,有点LEGB的感觉
print(Test.country)
person.more_information(True)
# Test.more_information(party_member=True)# 错误,类名后面不能跟成员属性或者方法
print(person.description())
print(Test.description())
print(person.readme)
print(person.laud)
print(person.income)
print(person.test())
print(Test.test())
西安市未央区
西安市
在构造器内定义类属性
在构造器内定义类属性
中国
中国
我来自中国陕西省(外来人口)西安市
中国陕西省(外来人口)西安市未央区
我的工资是10000
测试调用顺序:我是父类类方法
我来自中国陕西省(外来人口)西安市
我来自中国陕西省(外来人口)西安市
我来自中国陕西省(外来人口)西安市
作为一名中国人我很自豪
10000
测试调用顺序:我是父类类方法
None
测试调用顺序:我是父类类方法
None
class Prac(Test): # 该句话只继承了除构造器以外的代码,又成员属性定义在构造器内,故尚未继承成员属性。
    # 本类属性
    title = '测试如何调用本类及父类的属性和方法(本类类属性)'
    readme = '我来自中国(本类类属性)'
    country = '美国'  # 属性的重写
    
    def __init__(self, name, age, sex, district, native, party_member): 
        super(Prac, self).__init__(name, age, sex, district, native, party_member)# 继承父类构造器
        # 本类属性
        self.direction =  '我是测试人员(本类成员属性)'
        self.readme = '我来自中国陕西省西安市(本类成员属性)'
        self.age = 1000
        
    def test(self,):
        print('测试调用顺序:我是本类成员方法')
    
    
    def test_3(self,):
        print('检验本类成员方法')
    
    def print_information(self,):
        
        # self属性:本类和父类的类属性、成员属性均可通过self调用
        print(self.direction)  # 使用self调用本类成员属性
        print(self.title)  # 使用self调用本类类属性
        print('父类成员属性:', self.age)  # 使用self调用父类成员属性
        print('父类类属性:', self.country)  # 使用self调用父类类属性
        print("############self属性完毕####################")
        
        # 测试self调用属性顺序
        print(self.readme) # 先从本类成员属性找,再从父类成员属性找,然后从本类类属性找,最后从父类类属性找
        print('#####self属性调用顺序完毕######')
        
        # self方法:本类 
        self.test_3()  # 使用self调用本类成员方法
        self.test_4()  # 使用self调用本类类方法
        self.test_1()  # 使用self调用父类成员方法
        self.test_2()  # 使用self调用父类类方法
        print("############self方法完毕####################")        

        # 测试self调用方法顺序
        print(self.test()) # 先从本类类方法中找,再从本类成员方法找,然后从父类类方法找,最后从父类成员方法找
        # 本类和父类同时存在相同的类方法或者成员方法,其实是方法的重写,即多态性的一种体现
        print('#####self方法调用顺序测试完毕######')
        
        # super属性&方法(只用于调用父类)
        # print(super().age)  # 错误:不能使用super调用父类成员属性
        print(super().country) # 可以使用super调用类属性
        super().test_1()  # 可以使用super调用成员方法
        super().test_2()  # 可以使用super调用类方法
        super().test() # 如果父类类和成员有相同的方法,先调用父类方法,没有再找成员方法
        print("###############super完毕#############")
        
        # 父类.
        # print(Test.age)  # 不能使用父类.成员属性
        print(Test.country)  # 可以使用父类.类属性
        # Test.test_1()  # 不能使用父类.成员方法
        Test.test_2()  # 可以使用父类.类方法      
        
    @classmethod
    def test_4(cls,):
        print('检验本类类方法')

    @classmethod
    def test(cls,):
        print('测试调用顺序:我是本类类方法')
ex = Prac(name='张丽', age=30, sex='女', district='未央区', native='外来人口', party_member=True)
ex.print_information()
print(ex.country)
我是测试人员(本类成员属性)
测试如何调用本类及父类的属性和方法(本类类属性)
父类成员属性: 1000
父类类属性: 美国
############self属性完毕####################
我来自中国陕西省西安市(本类成员属性)
#####self属性调用顺序完毕######
检验本类成员方法
检验本类类方法
我是父类成员方法
我是父类类方法
############self方法完毕####################
测试调用顺序:我是本类类方法
None
#####self方法调用顺序测试完毕######
中国
我是父类成员方法
我是父类类方法
测试调用顺序:我是父类类方法
###############super完毕#############
中国
我是父类类方法
美国

5. java代码实例

package practice01;

public class test1 {
    // 前提说明:由于java所有的变量都需要提前声明,且同一作用域变量不能重复声明,故类属性和成员属性不可能同名(和python不一样)
    // 且所有的类属性和成员属性均需在(与public class处相差一个tab处) 进行定义。
    // 类方法和成员方法亦同:类方法和成员方法不可能同名

    // 父类和子类无论是类还是成员,亦无论属性还是方法,名称相同,即重写,是多态性的一种体现。


    // 类属性
    public static String country = "中国";
    public static String province = "陕西省";
    public static String city = "西安市";
    public static String readme;

    // 成员属性
    public String name;
    public int age;
    public String sex;
    public int income;
    public String sentence;
    public String laud;

    public test1(String name_, int age, String sex, String district, String native_, boolean party_member){
        name = name_;  // 当参数和成员属性名称不一样时,直接对成员属性进行赋值
        this.age = age;  // 当参数和成员属性名称不一样时,成员属性赋值时前面必须加this
        this.sex = sex;
        this.readme = "我是类属性"; // 在构造器内为类属性赋值,使用类.调用类属性
        this.sentence = this.description();  // 使用this调用类方法
        this.laud = test1.praise();  // 使用类.方式调用类方法
        this.income = salary(party_member);  // 直接调用成员方法
    }

    // 成员方法
    public int salary(boolean party_member){
        int money;
        if (party_member){
            money = 10000;
        }
        else{
            money = 11000;
        }
        return money;
    }

    public void test(){
        System.out.println("测试一下");
    }

    // 类方法
    public static String description(){
        String sent = "我来自" + country + province + test1.city; // 不能使用this.country,这里和python相类似,静态方法不能使用非静态变量
        return sent;
    }

    public static String praise(){
        String sent = "作为一名" + country + "人我很自豪";
        return sent;
    }

    public static void main(String[] args){
        test1 ex = new test1("Lucy", 15, "女", "未央区", "外来人口", true);
        System.out.println(ex.name);
        System.out.println(ex.age);
        System.out.println(ex.readme);
        System.out.println(ex.sentence);

    }
}
Lucy
15
我是类属性
我来自中国陕西省西安市
package practice01;

public class test2 extends test1 {

    public test2(String name_, int age, String sex, String district, String native_, boolean party_member, int rank){
        super(name_, age, sex, district, native_, party_member);   //构造器继承
        this.rank = rank;
        this.age = 10000;
    }

    public int rank;
    public int age;

    // 类属性重写
    public static String country = "美国";
    public static String sex = "不清楚";


    // 类方法的重写
    public static String description(){
        String sent = "我是子类类方法";
        return sent;
    }

    // 成员方法重写
    public void test(){
        System.out.println("再次测试一下");
    }

    // 调用父类
    public void print_(){
        System.out.println(this.income); // 调用父类成员属性
        System.out.println(this.age); // 父类和子类都有age,先从子类找
        System.out.println(super.age); // 调用父类成员属性
        System.out.println(test1.country); // 调用父类类属性
        System.out.println(this.city); // 调用父类类属性
        System.out.println(super.province); // 调用父类类属性
        System.out.println(this.sex);
        this.test();  // 父类和子类都有成员方法,先从子类找
        super.test(); // super指定从父类找,虽然这里用到了方法重写,但依然可以调用父类,多态性
    }

    public static void main(String[] args){
        test2 ex = new test2("Lucy", 15, "女", "未央区", "外来人口", true, 1);
        ex.print_();
    }

}
10000
10000
15
中国
西安市
陕西省
不清楚
再次测试一下
测试一下

标签:调用,java,成员,python,self,print,父类,对比,属性
来源: https://blog.csdn.net/weixin_43178406/article/details/104915572

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有