Comparing Object-Oriented Languages, Page 3
Public Class IntSquare
Dim squareValue As Integer
Property accessSquareValue() As Integer
Get
Return squareValue
End Get
Set(ByVal Value As Integer)
squareValue = Value
End Set
End Property
Public Function calculateSquare(ByVal Value As Integer) _
As Integer
System.Console.WriteLine(Value)
squareValue = Value * Value
Return squareValue>
End Function
End Class
Listing 1c: VB Code for IntSquare Class
Person
The Person example creates a simple class that includes the code to illustrate the object-oriented concept of encapsulation. You create attributes and the various accessor/mutator methods (getters and setters).
public class Person{
//Attributes
private String name;
private String address;
//Methods
public String getName(){
return name;
}
public void setName(String n){
name = n;
}
public String getAddress(){
return address;
}
public void setAddress(String adr){
address = adr;
}
}
Listing 2a: Java Code for Person Class
public class Person{
//Attributes
private string name;
private string address;
//Methods
public string getName(){
return name;
}
public void setName(string n){
name = n;
}
public string getAddress(){
return address;
}
public void setAddress(string adr){
address = adr;
}
}
Listing 2b: C# Code for Person Class
Public Class Person
Dim name As String
Dim address As String
Property accessName() As String
Get
Return name
End Get
Set(ByVal Value As String)
name = Value
End Set
End Property
Property accessAddress() As String
Get
Return address
End Get
Set(ByVal Value As String)
address = Value
End Set
End Property
End Class
Listing 2c: VB Code for Person Class
0 Comments (click to add your comment)
Networking Solutions
