例項023公有和私有

2021-10-05 02:38:20 字數 1171 閱讀 9980

module module1

public class a

private thisx as single '這是私有的

end class

public class b

inherits a

public sub ok()

dim x as integer

'thisx=10 這是錯誤的

x = 10

msgbox(x)

end sub

end class

public class 人員

private pname as string

'私有的方法,只能在人員類使用

private sub work()

msgbox("working", msgboxstyle.okonly, pname)

end sub

public writeonly property name as string

set(byval value as string)

pname = value

end set

end property

'公有方法

public sub speakoutname()

msgbox("my name is " & pname, msgboxstyle.information, "人員")

end sub

'公有方法

public sub gotowork()

msgbox("on the way ", msgboxstyle.okonly, pname)

'呼叫私有方法

work()

end sub

end class

public class 售貨員

inherits 人員

public sub setname(byval myname as string)

name = myname

end sub

end class

sub main()

dim mrlee as new 售貨員

mrlee.setname("李遠")

mrlee.speakoutname()

mrlee.gotowork()

end sub

end module

公有繼承和私有繼承的例項

include iostream.h include string.h class person 基類person定義 person char strname,bool b int nage 基類建構函式 void setname char strname 設定姓名 void set bool b ...

公有 私有和受保護

class base struct public derived public base ok i is protected derived classes can access i int use base2 error j is private struct protect drived pro...

C 繼承 公有,私有和保護

公有繼承 public 私有繼承 private 保護繼承 protected 是常用的三種繼承方式。1.公有繼承 public 公有繼承的特點是基類的公有成員和保護成員作為派生類的成員時,它們都保持原有的狀態,而基類的私有成員仍然是私有的,不能被這個派生類的子類所訪問。2.私有繼承 private...