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
中國
西安市
陝西省
不清楚
再次測試一下
測試一下