【从Java转C#】第三章:对象和类型

简介: 【从Java转C#】第三章:对象和类型

对象和类型

ref和out

  • 双方都可以改变原始的地址
  • 初始值的不同
  • ref:需要赋予变量初始值
  • out:不需要赋予变量初始值
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            int j = 1;
            Swap(ref i, j);
            Console.WriteLine("i : " + i);
            Console.WriteLine("j : " + j);
            Console.ReadLine();
        }
        static void Swap(ref int i, int j)
        {
            i = 100;
            j = 101;
        }
    }
}

参数的使用

  • 命名参数:可以自己改变形参的顺序
Swap(lastName: "lastName", firstName: "firstName");
 static void Swap(string firstName, string lastName)
  {
      Console.WriteLine(firstName);
      Console.WriteLine(lastName);
  }
  • 可选参数:参数是可以选择的,但是可以选择的函数【可用可不用】必须在定义的参数后
static void Swap(string lastName, string firstName = "231")
    {
        Console.WriteLine(firstName);
        Console.WriteLine(lastName);
    }

方法的重载

  • 方法名相同、参数的个数或类型不同、同一个类中
  • C#限制
  • 两个方法不能仅在返回类型上有区别
  • 两个方法不能根据参数是声明为ref还是out来区分
void display(string name1)
  {
        // *****
    }
 void display(string name1, string name2)
    {
        // *****
    }

属性

  • 正常的get/set
class student
    {
        private int _age;
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }
    }
  • 自动的实现:用于没有任何逻辑,单单实现功能而已
class student
    {
        private int _age;
        public int Age { get; set; }
    }

构造函数

  • 不书写任何的构造函数:引用类型:空引用、数值类型:0、bool:false
student st = new student();
 Console.WriteLine(st.age);
 class student
  {
     public int age;
  }
  • 书写带参的构造函数:在进行实例化时,就要实现带参
  • this:一般使用this来代表当前的属性
class student
    {
        public int age;
        public student(int age)
        {
            this.age = age;
        }
    }
  • 静态构造函数:构造函数只执行一次,只要创建类的对象,就会执行它
class student
    {
        public int age;
        static student()
        {
           // write your code
        }
    }
  • 构造函数调用其他的构造函数:有点方便~但我感觉多了容易乱、对于父类的调用,使用base
class student
    {
        private int age;
        private string name;
        public student(int age, string name)
        {
            this.age = age;
            this.name = name;
        }
        public student(int age)
        {
            this.age = age;
            this.name = "黄良帅";
        }
    }
======================================分割线=================================
   class student
    {
        private int age;
        private string name;
        public student(int age, string name)
        {
            this.age = age;
            this.name = name;
        }
        public student(int age):this(age, "黄良帅")
        {
        }
    }  
  • 只读字段【readonly】:可以在构造函数中给其赋值,但不能在其他地方赋值
readonly int a = 1;
 a  = 2; // 错误,找不到a
 public student()
   {
        a = 2;
   }

匿名类型

  • new的一样,Type就一样
var captain = new { firstName = "hls", lastName = "s" };
var doctor = new { firstName = "s", lastName = "s" };
Console.WriteLine(captain.GetType());
Console.WriteLine(doctor.GetType());
  • 也可以直接实例化对象
var capion = new student();

结构【Struct

  • 结构的特点
  • 结构是值类型,不是引用类型
  • 结构不支持继承
  • 对于结构,构造函数的工作方式有一些区别。尤其是编译器总是提供一个无参数的默认构造函数,它是不允许被替代的。
  • 使用结构,可以指定字段如何在内存中布局
  • 结构和类
  • 相同
doctor doc;
 student stu;
=============分割线==========
 doctor doc = new doctor();
 student stu = new student();
  • 不同
doctor doc;
 student stu;
 =============分割线==========
 doc.age = 1;
 stu.age = 1; // 错误:对于类来说,没有new【引用】,不能直接赋值

弱引用(WeakReference)

  • 个人感觉这一块和Java的GC回收差不多,基本参考强弱软虚
  • 这里等看了C#的垃圾回收再来填坑

静态类

  • 如果类只包含静态的方法和属性,这个类就是静态的
  • 一旦类被static修饰后,就不能使用实例创建
student stu = new student(); // 会报错
 static class student
    {
        public int age;
        public string name;
    }

Object

  • ToString 虚方法
  • Equals
  • GetHashCode
  • GetType
  • MemberwiseClone 复制对象


相关文章
|
1月前
|
Java
【Java】如果一个集合中类型是String如何使用拉姆达表达式 进行Bigdecimal类型计算?
【Java】如果一个集合中类型是String如何使用拉姆达表达式 进行Bigdecimal类型计算?
25 0
|
1月前
|
Java
java中的泛型类型擦除
java中的泛型类型擦除
13 2
|
29天前
|
Java
java8中List对象转另一个List对象
java8中List对象转另一个List对象
37 0
|
1月前
|
存储 Java
JAVA字符串与其他类型数据的转换
JAVA字符串与其他类型数据的转换
28 4
|
1天前
|
安全 Java 程序员
Java 泛型类型:变幻中的不变性
【4月更文挑战第21天】
4 1
Java 泛型类型:变幻中的不变性
|
3天前
|
设计模式 JavaScript Java
[设计模式Java实现附plantuml源码~行为型] 对象状态及其转换——状态模式
[设计模式Java实现附plantuml源码~行为型] 对象状态及其转换——状态模式
|
4天前
|
存储 Java
JAVA变量类型
JAVA变量类型
11 0
|
5天前
|
Java
Java基础之对象的引用
Java基础之对象的引用
6 0
|
9天前
|
存储 算法 安全
什么是Java泛型类型?
【4月更文挑战第13天】
12 0
什么是Java泛型类型?
|
9天前
|
Java
Java中如何克隆一个对象?
【4月更文挑战第13天】
15 0