栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

C#语法与Java语法差异

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#语法与Java语法差异

注释
/// 			注释函数和类,相当于java中/** /
VS快捷键
注释:   ctrl+k c 		弹起按 c
取消注释:ctrl+k u	   弹起按 u
对其代码:ctrl+k d	   弹起按 d
只能提示:ctrl+j
折叠代码:#region   xxx中间是被折叠的内容xxx   #endregion 
shift+home:		向左整行
shift+end:		向右整行
打印 读取 暂停 占位符
string name1 = "张三";	// 与java不一样,string首字母小写
string name2 = "李四";
Console.WriteLine("{0},{1}",name1, name2);	// 打印。占位多填无效 符少填报错
Console.ReadKey();	// 不暂停看不清控制台 启动会闪烁,输入任意键回车放行
string str = Console.ReadLine();	// 读取控制台输入信息
double a = 10.0 / 3;
Console.WriteLine("{0:0.00}", a);	// 保留两位小数
转义符
Console.WriteLine("好好学习n天天向上");	// n 表示换行 【window换行rn】
Console.ReadKey();
Console.WriteLine("好好学习t天天向上");	// t 表示空格
Console.ReadKey();
Console.WriteLine("好好学习b天天向上");	// b 表示删除前面一个字符
Console.ReadKey();
string path = @"C:Testdemoday1";	 // @ 路径转义
Console.WriteLine(path);				 // b 表示删除前面一个字符
Console.ReadKey();
字符转换
int a = 10;
int b = 3;
double c = a / b; // 3 与java不一样,可以存放整数
数据类型不兼容 转换
string str = "123";
double d = Convert.ToDouble(str);	// d = 132
常量
const int a = 1;
a = 2; 	// 编译错误
枚举
using System;
using System.Conllections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace enumeration

public enum Gender
{
    BOY, GIRL	// 如果给BOY赋值2 那么GIRL默认就是3
}

class Program
{ 
    static void Main(string[] args){
        int boy = (int)Gender.BOY;		// boy = 0 根据下标来转换的
        int a = 0;
        int b = 5;	
        Gender gender_boy = Gender.BOY;	// 枚举取值
        Gender a = (Gender)a;	// a = BOG 拿到下标为0的值
        Gender b = (Gender)b;	// b = 5 没有下标为5的枚举,不报错
        string str = "0"; // 数字类型不会报错 str=BOY,GIRL不会报错 字符找不到时会报错
        Gender gender_str = (Gender)Enum.Parse(typeof(Gender),str)
    }
}
结构 静态 方法
using System;
using System.Conllections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace structure

public struct Person			// struct结构不具备面向对象的三大特性
{
    public string _name;		// 字段 public修饰 Main方法体才能调用
    public int _age;			// 字段能存多个值 变量只能存一个值
    public Gender _gender;		// 下划线代表字段 规范写法
}

public enum Gender
{
    BOY, GIRL
}

class Program
{ 
    public static int _number = 10;		// static修饰 整个类都可以直接使用 模拟全局变量
    
    static void Main(string[] args){
        
        Person zsPerson;
        zsPerson._name = "张三";
        zsPerson._age = 21;
        zsPerson._gender = Gender.BOY;
        
        Person lsPerson;
        zsPerson._name = "李四";
        zsPerson._age = 22;
        zsPerson._gender = Gender.GIRL;
        
        public static void Test(_number)	// 与java不一样 不用再写类型
        {
            a = a + 5;
        }
    }
}
数组
int[] nums = {1,3,8,2,6};
Array.Sort(nums);		// 排序 升序
Array.Reverse(nums);	// 排序 降序
out
Test(10,out b,out c)	// 返回值 b = 12  c = 310

public static void Test(int a, out int b, out string c)		// 被out修饰的参数会返回,可修饰多个参数返回多个值
{
    b = 2 + a;	// out的每个参数必须赋值 否则编译错误
    c = 3 + a
}
---------------------------------------------------------------------------
bool b = int.TryParse("123", out num);	// 字符串转数字
// 模拟TryParse方法,转换成功返回true num是转换后的值,转换失败返回false num默认值0
public static bool MyTryParse(string str, out int num)
{
    num = 0;	// 设置默认值
    try			// 如果转换失败会报错,捕获异常处理
    {
        num = Convert.ToInt32(str);		// string类型 转 int类型
        return true;
    }
    catch
    {
        return false;
    }
}
ref
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    Test(ref a, ref b);		// ref修饰的参数 必须赋值
    Console.WriteLine(a);	// 510	被重新赋值了
    Console.WriteLine(b);	// 520
    Console.ReadKey();
    
}

// 被ref修饰的参数会返回,且直接赋值给对应参数
public static void Test(ref int a , ref int b)	
{
    a += 500;
}
get set
// 底层代码跟java一样
public string Name;
public string Name 
{
    get { return name; }
    set { name = value; }
}

public int Age 
{
    get;
    set;
}
params
// params修饰的参数是可变参数 必须放最后 否则编译错误

// 求任意整数长度数组的和
// 调用方法

getSum(1, 3, 8, 9, 16, 2);
// int[] nums = {1, 3, 8, 9, 16, 2}
// getSum(nums);

public static int getSum(params int[] num)
{
    int sum = 0;	// 总和
    for (int i = 0; i < num.Length; i++)
    {
        sum += n[i];
    }
    return sum;
}
this
// 构造中使用this
public Student(int id, string name, int age, string sex, string address)
{
    
}
// 这里的this指定了上面的构造 来赋值
public Student2(int id, string name):this(id, name, age, sex)
{
    //this.Id = id;
    //this.Name = name;
}
继承
// 冒号:表示基础
public class Children:Parent {}
析构函数
// 当程序结束时析构函数执行 立马释放资源
~Student()
{
    Console.WriteLine("我是析构函数");
}
里氏转换
// 相当于java中的多态
// is 判断类型转换 true false
bool flag = Children is Parent;		// flag = false
// 通过继承后 as 表示类型转换
Children children = Parent as Children; 
// 跟as差不多
Children children = (Children)Parent;
Hashtable
// 相当于java的map
Hashtable ht = new Hashtable();
ht.Add("张三", "男");
ht.Add("李四", "女");
Console.WriteLine(ht.keys);				// 拿到所有key
Console.WriteLine(ht.values);			// 拿到所有value
Console.WriteLine(ht[key]);				// 通过key拿到value
bool flag = ht.ContainsKey(key);	    // Contains 是否包含
Console.ReadKey();
Dictionary
// 跟Hashtable很像 多了个泛型
Dictionary dic = new Dictionary();
dic.Add("张三", "男");
dic.Add("李四", "女");
var
// var可以根据值 自动得到类型
var a = 10;
var b = "张三";

Console.WriteLine(a.GetType());		// Int32
Console.WriteLine(b.GetType());		// String
Console.ReadKey();
遍历
foreach(var item in ht.Keys)
{
    Console.WriteLine("key-{0},value-{1}", item, ht[item]);
}

foreach(KeyValuePair kv in dic)
{
    Console.WriteLine("key-{0},value-{1}", kv.Key, kv.Value);
}
虚方法
public virtual void hello() {}		// 父类virtual虚方法
public override void hello() {}		// 子类override重写
部分类
// 类名相同 相当于一个类 用partial修饰
public partial class Person 
{
    
}
public partial class Person 
{
    
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926560.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号