属性的引出
using System;
using System.Collections.Generic;
using System.Text;
namespace Attribute.demo
{
public class Animal
{
public int Age;
public bool Sex;
public double Weight;
public Animal(int age, double weight, bool sex)
{
this.Age = age;
this.Weight = weight;
this.Sex = sex;
}
public void Eat()
{
Console.WriteLine("Animal Eat");
}
public void Sleep()
{
Console.WriteLine("Animal Sleep");
}
public override string ToString()
{
return string.Format("Animal Age:{0},Weight:{1},Sex:{2}", Age, Weight, Sex);
}
}
}
using System;
namespace Attribute.demo
{
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal(1, 1.2, false);
Console.WriteLine("Animal Age:{0},Weight:{1},Sex:{2}", animal.Age, animal.Weight, animal.Sex);
animal.Age = -1;
animal.Weight = -1.2;
Console.WriteLine(animal);
}
}
}
//
Animal Age:1,Weight:1.2,Sex:False
Animal Age:-1,Weight:-1.2,Sex:False
- 动物的年龄,体重不在范围内
Data
变成private
,利用get{} set{}
方法
using System;
using System.Collections.Generic;
using System.Text;
namespace Attribute.demo
{
public class Animal
{
private int _age;
private bool _sex;
private double _weight;
public Animal(int age, double weight, bool sex)
{
_age = (age > 0) ? age : 0;
_weight = (weight > 0) ? weight : 0;
_sex = sex;
}
public int GetAge()
{
return _age;
}
public void SetAge(int value)
{
if (value <= 0)
throw new ArgumentOutOfRangeException();
_age = value;
}
public bool GetSex()
{
return _sex;
}
public double GetWeight()
{
return _weight;
}
public void SetWeight(double value)
{
if (value <= 0)
throw new ArgumentOutOfRangeException();
_weight = value;
}
public void Eat()
{
Console.WriteLine("Animal Eat");
}
public void Sleep()
{
Console.WriteLine("Animal Sleep");
}
public override string ToString()
{
return string.Format("Animal Age:{0},Weight:{1},Sex:{2}", _age,_weight,_sex);
}
}
}
using System;
namespace Attribute.demo
{
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal(1, 1.2, false);
Console.WriteLine("Animal Age:{0},Weight:{1},Sex:{2}",animal.GetAge(),animal.GetWeight(),animal.GetSex() );
animal.SetAge(-1);
animal.SetWeight(-1.2);
Console.WriteLine(animal);
}
}
}
//
Animal Age:1,Weight:1.2,Sex:False
程序会报错
- 属性的提出可以对
get与set方法进行缩写
using System;
using System.Collections.Generic;
using System.Text;
namespace Attribute.demo
{
public class Animal
{
private int _age;
private bool _sex;
private double _weight;
public Animal(int age, double weight, bool sex)
{
_age = (age > 0) ? age : 0;
_weight = (weight > 0) ? weight : 0;
_sex = sex;
}
public int Age
{
get{ return _age; }
set{ _age = (value > 0) ? value : 0; }
}
public bool Sex
{
get{ return _sex;}
}
public double Weight
{
get{ return _weight; }
set { _weight = (value > 0) ? _weight : 0; }
}
public void Eat()
{
Console.WriteLine("Animal Eat");
}
public void Sleep()
{
Console.WriteLine("Animal Sleep");
}
public override string ToString()
{
return string.Format("Animal Age:{0},Weight:{1},Sex:{2}", _age,_weight,_sex);
}
}
}
using System;
namespace Attribute.demo
{
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal(1, 1.2, false);
Console.WriteLine("Animal Age:{0},Weight:{1},Sex:{2}",animal.Age,animal.Weight,animal.Sex );
animal.Age = -1;
animal.Weight = -1.2;
Console.WriteLine(animal);
}
}
}
//
Animal Age:1,Weight:1.2,Sex:False
Animal Age:0,Weight:0,Sex:False
索引器
- 索引器的定义:
indexer
是集合类中的一种特殊属性,可使得集合类中的元素像数组元素一样访问 - 语法结构
public 元素类型 this[int index]
{
get{...}
set{...}
}
public 元素类型 this[string name]
{
get{...}
set{...}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Indexer.demo
{
public class Student
{
private long _id;
private string _name;
public long ID
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = string.IsNullOrEmpty(value) ? "Null" : value; }
}
public Student(long id, string name)
{
_id = id;
_name = name;
}
public override string ToString()
{
return string.Format("Student ID:{0},Name:{1}",_id,_name);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Indexer.demo
{
public class StudentSet
{
private int _count;
private readonly int _maxCount = 500;
private readonly Student[] _stus;
public int Count
{
get { return _count; }
}
public Student this[int index]
{
get
{
if (index < 0 || index > _count - 1)
throw new IndexOutOfRangeException();
return _stus[index];
}
set
{
if (index < 0 || index > _count - 1)
throw new IndexOutOfRangeException();
if (value == null)
throw new ArgumentNullException();
_stus[index] = value;
}
}
public void Add(Student stu)
{
if (stu == null)
throw new ArgumentNullException();
if (_count == _maxCount)
throw new ArgumentOutOfRangeException();
_stus[_count] = stu;
_count++;
}
public StudentSet()
{
_count = 0;
_stus = new Student[_maxCount];
}
}
}
using System;
namespace Indexer.demo
{
class Program
{
static void Main(string[] args)
{
StudentSet stuSet = new StudentSet();
stuSet.Add(new Student(10086, "张三"));
stuSet.Add(new Student(95988, "李四"));
stuSet[1].Name = string.Empty;
Console.WriteLine(stuSet.Count);
Console.WriteLine(stuSet[0]);
Console.WriteLine(stuSet[1]);
Console.WriteLine(stuSet[2]);//报错
}
}
}
//
2
Student ID:10086,Name:张三
Student ID:95988,Name:Null
接口interface
- 定义:接口是类设计的蓝图,即只提供声明而没有实现
- 接口不可以直接实例化对象(与抽象类相同)
- C#允许一个类实现多个接口(注意与继承的区别)
- 接口就是包含一系列不被实现的方法,而把这些方法的实现交给继承它的类
- 接口的表示
ex1
- 接口里不需要加限制修饰符
//interface
using System;
using System.Collections.Generic;
using System.Text;
namespace Interface.demo
{
public interface IAnimal
{
int Age { get; set; }
double Weight { get; set; }
void Eat();
void Sleep();
}
}
//class
using System;
using System.Collections.Generic;
using System.Text;
namespace Interface.demo
{
public class Dog : IAnimal
{
private int _age;
private double _weight;
public int Age
{
get { return _age; }
set { _age = (value > 0) ? _age : 0; }
}
public double Weight
{
get { return _weight; }
set { _weight = (value > 0) ? _weight : 0; }
}
public void Eat()
{
Console.WriteLine("Dog Eat");
}
public void Sleep()
{
Console.WriteLine("Dog Sleep");
}
public override string ToString()
{
return string.Format("Dog Age:{0},Weight:{1}", Age, Weight);
}
}
}
using System;
namespace Interface.demo
{
class Program
{
static void Main(string[] args)
{
IAnimal animal = new Dog();
animal.Age = 1;
animal.Weight = 2.5;
Console.WriteLine(animal);
animal.Eat();
animal.Sleep();
}
}
}
//
Dog Age:1,Weight:2.5
Dog Eat
Dog Sleep
接口interface
与抽象类abstract class
的异同
相同点:
- 接口与抽象类都不可以直接实例化对象
不同点:
- 抽象类中的数据和操作必须有限制修饰符,而接口中的数据和操作不可以有限制修饰符
- 抽象类中可以有带实现体的方法(非
abstract方法
),而接口只能有方法的声明 - 抽象类在子类中通过
override
关键字覆写抽象方法,而接口被子类直接实现
一个类可以实现多个接口,但要注意多个接口中有重名方法的处理
ex1
//interface略
既实现第一个接口里面的重名方法
也实现第二个接口里面的重名方法
using System;
using System.Collections.Generic;
using System.Text;
namespace DoubleInterface
{
public class Worker : IHighWayWorker, IRailWayWorker
{
public void Build()
{
Console.WriteLine("HighWay,RailWay,Build");
}
public void HighWayOperation()
{
Console.WriteLine("HighWayOperation");
}
public void RailWayOperation()
{
Console.WriteLine("RailWayOperation");
}
}
}
ex2
//
分别实现
语法规定:不加限制修饰符
using System;
using System.Collections.Generic;
using System.Text;
namespace DoubleInterface
{
public class Worker : IHighWayWorker, IRailWayWorker
{
void IHighWayWorker.Build()
{
Console.WriteLine("HighWay,Build");
}
void IRailWayWorker.Build()
{
Console.WriteLine("RailWay,Build");
}
public void HighWayOperation()
{
Console.WriteLine("HighWayOperation");
}
public void RailWayOperation()
{
Console.WriteLine("RailWayOperation");
}
}
}
泛型
- 存放多种数据类型
using System;
namespace Genericity.demo
{
public class GSet
{
private object[] objs = new object[200];
public void Insert(int k, object val)
{
objs[k] = val;
}
public object Locate(int k)
{
return objs[k];
}
}
class Program
{
static void Main(string[] args)
{
GSet gset = new GSet();
gset.Insert(0, 123);
gset.Insert(1, "abc");
int abc = (int)gset.Locate(1);
//运行时出错,类型安全问题
}
}
}
- 泛型定义:即参数化类型
- 在编译时用一个具体类型代替该参数类型,可定义类型安全的类而不影响工作效率
using System;
namespace Genericity.demo1
{
public class GSet<T>
{
private readonly int _maxSize;
private readonly T[] _set;
public GSet()
{
_maxSize = 100;
_set = new T[_maxSize];
}
public void Insert(int k,T x)
{
_set[k] = x;
}
public T Locate(int k)
{
return _set[k];
}
}
class Program
{
static void Main(string[] args)
{
GSet<int> gSet1 = new GSet<int>();
gSet1.Insert(0, 123);
int k1 = gSet1.Locate(0);
Console.WriteLine(k1);
GSet<string> gSet2 = new GSet<string>();
gSet2.Insert(0, "abc");
string k2 = gSet2.Locate(0);
Console.WriteLine(k2);
}
}
}
//
123
abc
Class Diagram
- 依赖关系
Dependence
在类方法的形参中体现
//Oxygen 和 Water 的 Class略
using System;
using System.Collections.Generic;
using System.Text;
namespace Dependence.demo
{
public abstract class Animal
{
public int Age;
public double Weight;
public abstract void Breed();
public abstract void Eat();
public abstract void Metabolism(Oxygen o2, Water water);
public abstract void Sleep();
}
}
- 关联关系
Association
在属性中体现
- 继承关系
Inheritance
- 实现关系
realize
:类与接口之间的实现关系