(1)建立學生student類,它包含三個屬性:整型屬性學號(no)、字元型屬性姓名(name)、日期型屬性生日(birthday),乙個輸出的方法printinformation,其中,輸出資訊包含學號、姓名、生日。
(2)把1題中增加年齡(age)屬性,該屬性唯讀,輸出資訊為:學號、姓名、年齡。
(3)把2題中定義乙個建構函式,該建構函式可以包含(學號、姓名、生日)
public class studentsdim intno as integer
dim strname as string
dim datbirthday as date
public structure studentsinfo
dim no as integer
dim name as string
dim birthday as date
end structure
public sub new()
intno = 0
strname = ""
datbirthday = now
end sub
public function printinformation() as studentsinfo
dim stuinformation as studentsinfo
with stuinformation
.no = intno
.name = strname
.birthday = datbirthday
end with
return stuinformation
end function
public property no()
getreturn intno
end get
set(byval value)
intno = value
end set
end property
public property name()
getreturn strname
end get
set(byval value)
strname = value
end set
end property
public property birthday()
getreturn datbirthday
end get
set(byval value)
datbirthday = value
end set
end property
end class
public class studentsaddage
inherits students
dim intage as integer
public structure studentsinfoaddage
dim no as integer
dim name as string
dim age as integer
end structure
public sub new()
intage = 0
end sub
public property age()
getreturn intage
end get
set(byval value)
intage = value
end set
end property
public overloads function printinformation(byval isprintage as boolean) as studentsinfoaddage
dim stuinformation as studentsinfoaddage
with stuinformation
.no = mybase.no
.name = mybase.name
.age = intage
end with
return stuinformation
end function
end class
public class studentssubnew
inherits studentsaddage
public sub new(byval no as integer, byval name as string, byval birthday as date)
mybase.no = no
mybase.name = name
mybase.birthday = birthday
end sub
end class
Python 封裝物件類
python雖然是解釋型語言,但從設計之初就已經是一門物件導向的語言,對於python來說一切皆為物件。正因為如此,在python中建立乙個類和物件是很容易的,當然如果習慣面向過程或者函式的寫法也是可以的,python並不做硬性的限制。在很多場景,我們都需要封裝乙個物件類,這樣操作起來很方便快捷,接...
類和物件封裝
屬性和行為 案例 設計學生類 訪問許可權 class 和 struct 的區別 成員屬性私有化 設計案例1 立方體類 設計案例2 點和圓關係 1 屬性和行為 includeusing namespace std const double pi 3.14 c 物件導向的三大特性 封裝 繼承 多型 萬事...
類和物件(封裝)
c 物件導向的三大特性 封裝,繼承,多型。c 認為萬物皆為物件,物件上由屬性和行為。具有相同性質的物件,我們可以抽象為類。封裝 將屬性和行為作為乙個整體,表現生活中的事物 將屬性和行為加以許可權控制。封裝意義一 在設計類的時候,屬性和行為寫在一起,表現事物 語法 class類名 類中的屬性和行為統一...