Morning Meeting Shares Java and. NET Themes

Empty cup mentality

Don't be afraid, don't worry, as long as you do, it will be easier to start;
Don't complain, don't scorn, summarize and learn from the comparison, you will have a deeper understanding of the knowledge you have mastered.

Date processing

Date processing in C # is nothing more than a conversion between two types, string and DateTime.

string dateStr = "2017-10-13";
//string to DateTime
DateTime date1 = Convert.ToDateTime(dateStr);
DateTime date2 = DateTime.Parse(dateStr);
WriteLine(dateStr);
//DateTime to string, the first string parameter can be any date format you want to render, and the second parameter is to eliminate the time zone effect.
string dateStr1 = date1.ToString("yyyy/MM/dd",CultureInfo.InvariantCulture);
WriteLine(dateStr1);

Date processing in Java is relatively slightly more complex

    public static Date string2Date(String dateString, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        ParsePosition pos = new ParsePosition(0);
        return format.parse(dateString, pos);
    }

    public static String date2String(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

SimpleDateFormat and ParsePosition are both required to introduce the java.text package.

Determine what type of current object is

C#, (1) Use the Type type GetType() method

Person person = new Person();
Type t2 = person.GetType();
WriteLine(t2.ToString());

(2) using is expression

bool result = person is Person;
WriteLine(result);

Java, using instanceof, for example, has a variable a, to determine whether it is a Person class, the code is as follows:

a instanceof Person

loop

enumeration

Simple enumeration

C#:

enum Colors
{
    Red,
    Green,
    Blue
}

Java:

enum Colors
{
    Red,
    Green,
    Blue
}
Enumeration with Constants

C#:

enum Colors
{
    Red = 1,
    Green = 2,
    Blue = 3
}

Java:

public enum Colors
{
    Red(1), Green(2), Blue(3);

    private int code;

    private setCode(int code)
    {
        this.code = code;
    }

    public int getCode()
    {
        return code;
    }
}
Enumeration with Constants and Descriptions

C#:

enum Colors
{
    [Description("red")]
    Red = 1,
    [Description("green")]
    Green = 2,
    [Description("blue")]
    Blue = 3
}
//C# Gets the constant corresponding to the enumeration
int code=(int)Colors.Red;
//C# Get Enumeration
Colors color = (Colors)2;
//C# Gets the corresponding description of the enumeration
string desc = GetEnumDesc(Colors.Blue);

        /// <summary>
        /// Get the description information for the enumeration
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string GetEnumDesc(Enum e)
        {
            FieldInfo field = e.GetType().GetField(e.ToString());
            return ((DescriptionAttribute)Attribute.GetCustomAttribute(field,
                   typeof(DescriptionAttribute))).Description;

        }

Java:

public enum Colors
{
    Red(1,"red"), Green(2,"green"), Blue(3,"blue");

    private int code;
    private String description;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    private Color(int code,String description){
        this.code=code;
        this.description=description;
    }

    public static String getDescription(int code){
        for(Color color :Color.values()){
            if(color.getCode()==code){
                return color.description;
            }
        }return null;
    }
}

String co=Colors.getDescription(2);
System.out.println(co);

Attribute and reflection are needed to obtain enumeration description in C #.
When you get an enumeration description in Java, just get it according to the attributes of the class.
The enumeration of C# is the value type, and the enumeration of Java is the reference type.

Method parameter transfer

Java basically refers to one way:

Fun(T t);

C# is flexible because it can also have ref parameters, out parameters, optional parameters, etc.

Modifiers for object attributes

When C# defines an entity, the attributes in the entity are basically public, so that the attributes can be pointed at when the object is instantiated.
Java is defined as private, and then gets or sets attribute values through public get and set methods.
However, if you look at the compiled intermediate language IL of C#, you will find that the implementation of C# is the same as that of Java.

switch Statements

There's nothing different about this.

abstract class

It doesn't seem to make any difference.

Interface

All members of the C interface do not need to write modifiers. The default modifier is public. If you want to use private or protected to modify the member methods in the interface, Universe First IDE will prompt you to say that modifiers are invalid for this item.

public interface ITest
{
    void Test();
    bool IsTrue();
}

The members of the Java interface need to write public modifiers and cannot be modified with private or protected.

public interface ITest
{
    public void Test();
    public bool IsTrue();
}

inherit

C#:

public class Class1 :BaseClass
{
      //Class internal variables and methods
}

Java:

public class Class1 extends BaseClass
{
      //Class internal variables and methods
}

ORM

C # ORM has EF, dapper, etc.
Java ORM has mybatis, DbUtils, etc.
However, some colleagues have used mybatis in. NET projects.
When it comes to architecture, I'm confused.

reflex

For me, it's a muddled-faced content, except that there's something called Autofac in. NET.

From the first year of my work, I began to read the relevant articles, but I never wrote the relevant code myself, so that I still know little about it. For a programmer, this knowledge is probably necessary for professional accomplishment. Maybe I am not a qualified programmer. But I like my job! So, go on, boy!

Some references:
How to: Convert a String to a DateTime (C# Programming Guide)
C# typeof() and GetType() differences
The usage of new in C # and the difference from override
NET ORM
Analysis and Use of.Net Reflection Mechanism
Recommend a good website: codeproject

Keywords: Java Attribute Mybatis Programming

Added by minorgod on Mon, 20 May 2019 01:24:27 +0300