- StringBuilder构造器
- 枚举类型的应用
- 类的字段
- 类的属性
- 类的构造函数
- 类的方法
MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class MyClass
{
public void Show( )
{
Console.WriteLine( "这是一个简单的方法....");
}
public void Show( string msg )
{
Console.WriteLine( "您有新到消息:{0}" , msg );
}
public int GetLength( int width , int height )
{
int length = ( width + height ) * 2;
return length;
}
}
}
Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public enum ProductType
{
数码,手机,零食,化妆品,服装
}
public class Product
{
public int Id {
get; set; }
public string Name {
get; set; }
public Decimal Price {
get; set; }
public ProductType Type {
get; set; }
public DateTime Birthday {
get; set; }
public int Count {
get; set; }
//无参构造方法
private Product( )
{
Id = 100;
Name = "iPhone X";
Price = 8888;
Type = ProductType.手机;
Birthday = new DateTime( 2018 , 3 , 4 );
}
//有参构造方法
public Product( int id , string name , Decimal price , ProductType type , DateTime birthday )
{
this.Id = id;
this.Name = name;
this.Price = price;
this.Type = type;
this.Birthday = birthday;
this.Count = 30;
}
//析构方法,对象被销毁前自动调用
~Product( )
{
Console.WriteLine( "对象即将被销毁......");
}
}
}
Shop.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Shop
{
public Product[ ] products {
get; set; }
public Shop( int length )
{
this.products = new Product[ length ];
}
//统计库存商品的总价值
public decimal GetTotalMoney( )
{
decimal money = 0;
//循环访问商品数组,然后计算总价值
foreach ( var p in this.products )
{
if ( p != null ) //确保商品有效
{
money += p.Price * p.Count;
}
}
return money;
}
//进货,将商品参数添加到数组中
public string Add( Product pro )
{