类是有成员的。比如一个人的姓名、性别、身高都是这个人的成员。 public class Human public Name as String public Gender as String public Stature as integer ''''我们假设身高以厘米为单位 end class
类是有方法的。比如一个人要吃东西,要睡觉,生病了还要看医生。 public class Human public Name as String public Gender as String public Stature as integer
public sub Eat() end sub
public sub Sleep() end sub
public sub SeeADoctor() end sub end class
方法有时候需要返回东西。比如生小孩: public function Born() as Human return new Human end function
嗯……不对,应该这样写: public function Born() as Human if Gender = "女" then return new Human else return nothing ''''VB.net中的nothing就像C++中的NULL,表示什么都没有。 end if end function
只有女人才能生小孩嘛。 到这里你可能想要一个完整的代码例子。代码如下: imports System
public module MyModule sub Main dim LaoWang as Human LaoWang = new Human LaoWang.Name = "老王" LaoWang.Gender = "男" LaoWang.Stature = 177 Console.writeline("{0}, {1}, 身高{2}厘米", _ LaoWang.Name, LaoWang.Gender, LaoWang.Stature) dim XiaoWang as Human = LaoWang.Born() Console.Read end sub end module
public class Human ''''这里是Human类的定义 public Name as String public Gender as String public Stature as integer
public sub Eat() end sub
public sub Sleep() end sub
public sub SeeADoctor() end sub
public function Born() as Human if Gender = "女" then return new Human else return nothing end if end function end class