看板Programming
标 题请问vb.net的继承
发信站KKCITY (Tue Oct 23 07:02:30 2007)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ncu!news.yzu!zoonews.ee.ntu!new
请问以下的继承程式,如果祖父有一百万
大儿子得到50万,二儿子得到剩下的40%,也就是20万
而大儿子的儿子(也就是孙子)他可以得到大儿子的一半,也就是25万
请问我该如何设计呢?
因为我原本的设计,孙子会得到6万...
谢谢!
附上语法如下:
Public Class Father
Public Shared Money As Long = 1000000
End Class
Public Class Son
Inherits Father '3 宣告必须放在最前面
Public money2 As Long
Public Sub New()
MyClass.money2 = MyBase.Money * 0.4
MyBase.Money -= MyClass.money2
MsgBox("son跑完後 father剩下" & MyBase.Money)
End Sub
End Class
Public Class Son2
Inherits Father
Public money2 As Long
Public Sub New()
MyClass.money2 = MyBase.Money * 0.5
MyBase.Money -= MyClass.money2
MsgBox("son2跑完後 father剩下" & MyBase.Money)
End Sub
End Class
Public Class GrandSon '继承son 而非 father
Inherits Son
Public money3 As Long
Public Sub New()
MyClass.money3 = MyBase.money2 * 0.5
MyBase.Money -= MyClass.money3
MsgBox("grandson跑完後 father剩下" & MyBase.Money)
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.E
ventArgs) Handles Button1.Click
Dim John As New Son
MsgBox("我有" & John.money2 & " 元!!")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.E
ventArgs) Handles Button2.Click
Dim jason As New Son2
MsgBox("我有" & jason.money2 & " 元!!")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.E
ventArgs) Handles Button3.Click
Dim Jimy As New GrandSon
MsgBox("我有" & Jimy.money3 & " 元!!")
End Sub
End Class
--
┌─────◆KKCITY◆─────┐ KKCITY 市民小密技 -文章标题上- ◣
│ bbs.kkcity.com.tw │ Ctrl+y 可设定单篇文章加密
└──《From:218.169.107.194
》──┘ ◤ Shift+y 可设定单篇文章密码
--
1F:推 adrianshum:我觉得这样写好像违背 inheritence 的 202.22.246.26 10/23 13:43
2F:→ adrianshum:本意... 202.22.246.26 10/23 13:43