• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#学习笔记(四)中级 类 类的成员属性 只读属性和索引 再说索引 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

终于到中级了  很开心哈

 

第十六讲 类

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]
}

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#操作iis启用父目录发布时间:2022-07-13
下一篇:
yield(C# 参考)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap