In the previous article, how does Java quickly boil to C #? (1): similar methods. We started one of the topics in C # and Java: similar methods. In this section, we will illustrate how similar it is.
Let's use a simple factory model example to explain how to review the design model.
First, you need to add the following monthly invitations:
1. Language Comparison: the Java file name must match the class name. In Java, the file name must follow the class name. In C #, although this is usually the case, it is not necessary. In C #, there can be two classes in one file, and starting from C #2.0, one class may span multiple files.
2. Simple Factory Mode
The Simple Factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the class it returns has a public parent class and a public method.
Let's look at a class diagram of C #/Java.
In terms of the class chart itself, except the case sensitivity of String and string, there is no difference. Both charts are drawn using Powerdesigner, but the templates are c #2.0 and Java respectively.
Base Class: Namer
Derived classes: FirstFirst and LastFirst both inherit Namer
NameFactory implements the Create method of Namer. In this example, It is getName ().
The Code comparison is as follows:
Namer (C #):
View plaincopy to clipboardprint?
Using System;
Namespace NameFactory
{
/// <Summary>
/// Summary description for Namer.
/// </Summary>
// Base class for getting split names
Public class Namer
{
// Parts stored here
Protected string frName, lName;
// Return first name
Public string getFrname ()
{
Return frName;
}
// Return last name
Public string getLname ()
{
Return lName;
}
}
}
Using System;
Namespace NameFactory
{
/// <Summary>
/// Summary description for Namer.
/// </Summary>
// Base class for getting split names
Public class Namer
{
// Parts stored here
Protected string frName, lName;
// Return first name
Public string getFrname ()
{
Return frName;
}
// Return last name
Public string getLname ()
{
Return lName;
}
}
}
Namer (Java ):
View plaincopy to clipboardprint?
Package com. factory. SimpleFactory;
Public class Namer {
Protected String frName, lName;
// Return first name
Public String getFrname ()
{
Return frName;
}
// Return last name
Public String getLname ()
{
Return lName;
}
}
Package com. factory. SimpleFactory;
Public class Namer {
Protected String frName, lName;
// Return first name
Public String getFrname ()
{
Return frName;
}
// Return last name
Public String getLname ()
{
Return lName;
}
}
FirstFirst (C #):
View plaincopy to clipboardprint?
Using System;
Namespace NameFactory
{
/// <Summary>
/// Summary description for FirstFirst.
/// </Summary>
Public class FirstFirst: Namer
{
Public FirstFirst (string name)
{
Int I = name. IndexOf ("");
If (I> 0)
{
FrName = name. Substring (0, I). Trim ();
LName = name. Substring (I + 1). Trim ();
}
Else
{
LName = name;
FrName = "";
}
}
}
}
Using System;
Namespace NameFactory
{
/// <Summary>
/// Summary description for FirstFirst.
/// </Summary>
Public class FirstFirst: Namer
{
Public FirstFirst (string name)
{
Int I = name. IndexOf ("");
If (I> 0)
{
FrName = name. Substring (0, I). Trim ();
& Nb