zoukankan      html  css  js  c++  java
  • C#学习笔记(四)中级 类 类的成员属性 只读属性和索引 再说索引

    终于到中级了  很开心哈

    第十六讲 类

    C#是一种面向对象语言 一切事物都是对象:废话

    类 对象 对象是类具体化,实例化。

    Person thePerson= new Person(); //为他开辟空间
    定义一个类:class

    访问限制关键字(public)
    public class Person
    {
    //成员属性
    public String name;
    public int age;
    public int shengao;
    //成员方法
    public string say{...}

    代码:
    /*
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace First {
    //类Form1代表窗口
    public partial class Form1 : Form {
    public Form1() {
    InitializeComponent();
    }
    // 主窗口的Load时间处理函数 不能被我们调用
    只能由操作系统来调用
    private void Form1_Load(object sender,
    EventArgs e) {
    Person zhangSan = new Person();
    zhangSan.name = "张三";
    this.textBox1.Text = zhangSan.say();

    }
    public class Person
    {
    //成员属性
    public String name;
    public int age;
    public int shengao;
    //成员方法
    public string say()
    {
    return "哥们儿,看在党国的面子上拉兄弟一把吧
    。";
    }
    }

    }
    }
    */

    第十七讲 类的成员属性

    属性:为了更好的访问“类”中的成员变量。C#提供了属性
    所谓“更好”值得是更加安全、清楚等。保护好私有变量的隐蔽性等

    public是公开的 在任何地方都可以访问

    给私有变量增加属性
    public class Person
    {
    private string name="";
    public string Name{
    get{return name;} //得到变量
    set{name=value;} //设置变量
    }
    }

    代码是一一对应的(默认是第三个)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace First {
    //类Form1代表窗口
    public partial class Form1 : Form {
    public Form1() {
    InitializeComponent();
    }
    // 主窗口的Load时间处理函数 不能被我们调用
    只能由操作系统来调用
    private void Form1_Load(object sender,
    EventArgs e) {
    /*
    Person zhangSan = new Person();
    zhangSan.name = "大郎";
    this.textBox1.Text = zhangSan.name;
    //*/
    /*
    Person zhangSan = new Person();
    zhangSan.Name = "张三";
    this.textBox1.Text = zhangSan.Name;
    //*/
    Person zhangSan = new Person();
    zhangSan.Name = "三";
    //zhangSan.Name = "张三";
    this.textBox1.Text = zhangSan.Name;

    }
    /*
    public class Person
    {
    public String name = ""; //共有变量
    ,可以在任何地方访问
    }

    }
    //*/
    /*
    public class Person {
    private String name = ""; //私有变量
    ,不能为外界访问
    public String Name {
    get { return name; }
    set { name = value; }
    }

    }
    //*/
    public class Person {
    private String name = ""; //私有变量
    ,不能为外界访问
    public String Name {
    get { return name; }
    set {
    if (value.Length >=
    2)
    name =
    value;
    else
    name = "无名
    氏";
    }
    }

    }

    }
    }


    第十八讲 只读属性和索引

    只读属性

    public class Person
    {
    private string name="";//私有变量
    public string Name
    {
    get{return name;}// 如果只有get属性 那么为只读属性, 只写属
    性为 set
    }
    }

    索引: 通过提供所引器,可以象处理数组一样处理对象

    public class Book
    {
    // 成员变量
    public string temp="";

    //成员索引
    public String this [int index] //this表示的是类名 string 索
    引必须有返回类型 即字符串
    { get{return temp;}
    set{temp=value;}
    }

    代码: 不是完全懂

    namespace First {
    //类Form1代表窗口
    public partial class Form1 : Form {
    public Form1() {
    InitializeComponent();
    }
    // 主窗口的Load时间处理函数 不能被我们调用
    只能由操作系统来调用
    private void Form1_Load(object sender,
    EventArgs e) {

    Book theBook = new Book();
    theBook[0] = "从前有一座山,山里有。
    。。";
    this.textBox1.Text = theBook[0]; //
    执行get部分
    }

    public class Book {
    public string temp = "";
    public String this[int index] {
    get { return temp; }
    set { temp = value; }
    }
    }
    }
    }

    第十九讲 再说索引

    索引:处理数组一样处理对象

    telephoneList theTelephoneList=new telephoneList()
    string name=

    public class Student
    {
    public String name;
    public int ID;
    }
    public class telephoneList
    {
    //成员变量
    private Student[] Students;
    public String this[int theID]
    }

    完全没听懂 代码也不完全 改天专门研究索引

  • 相关阅读:
    Jzoj4765 Crisis
    Jzoj4764 Brothers
    Jzoj4764 Brothers
    Jzoj4756 幻象
    Jzoj4756 幻象
    Jzoj4755 快速荷叶叶变换
    Jzoj4755 快速荷叶叶变换
    力扣算法题—059螺旋矩阵
    力扣算法题—058最后一个单词长度
    力扣算法题—057插入区间
  • 原文地址:https://www.cnblogs.com/cheshui/p/2363419.html
Copyright © 2011-2022 走看看