How can Java be quickly compiled into C #? (1): similar methods (2)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.