zoukankan      html  css  js  c++  java
  • 静态变量和实例变量

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 静态方法和实例方法
    {
        class Program
        {
            class Account
            {
                private decimal kucun;
                private string name;
                public Account(string name)
                {
                    this.kucun = 0;
                    this.name = name;
                    Print("商店名", 0);
                }
    
                public bool Desposit(decimal jine)
                {
                    if (jine <= 0)
                        return false;
                    kucun += jine;
                    Print("进货", jine);
                    return true;
                }
    
                public bool WithDraw(decimal jine)
                {
                    if (jine > kucun || jine <= 0)
                        return false;
                    kucun -= jine;
                    Print("出货", jine);
                    return true;
                }
    
                private void Print(string operation, decimal shuliang)
                {
                    Console.WriteLine("店名:{0}", name);
                    Console.WriteLine("{0}:{1}", operation, shuliang);
                    Console.WriteLine("库存:{0}", kucun);
                    Console.WriteLine("============================");
                    Console.ReadKey();
                }
    
                public static void xinxi()
                {
                    Console.WriteLine("    电话:12345678912");
                    Console.WriteLine("===========================");
                    Console.WriteLine("    质量第一 价格最低");
                    Console.WriteLine("===========================");
                }
    
                static void Main(string[] args)
                {
                    /*
                  静态方法实例方法:C#中使用static关键字修饰的方法称为静态方法,而没有使用static修饰的方法称为实例方法。
                 * 静态方法的成员比较特殊,不属于一个类的某个具体实例或者对象。而是属于类本身。在静态方法中不能使用关键
                 * 字this并且只能访问类中的静态成员,而不能使用实例成员。在项目文件访问静态方法时只能使用类名,而不需要
                 * 创建额外的对象。实例方法可以调用类的所有成员,在调用实例方法时必须使用类的实例或者对象,来引用,并且
                 * 在实例方法内可以this关键字。
                 */
                    Account.xinxi();
                    Account q = new Account("商店!");
                    bool succed = q.Desposit(100);
                    if (!succed)
                        Console.WriteLine("进货失败!!");
                    succed = q.WithDraw(50);
                    if (!succed)
                        Console.WriteLine("出货失败!");
                    Console.ReadKey();
                }
            }
        }
    }
  • 相关阅读:
    DBCP数据源使用
    实例介绍Cocos2d-x开关菜单
    Material UI:很强大的CSS框架
    stl之hash_multimap
    使用OpenCV滑动条写成的简单调色器,实时输出RGB值
    Android中怎样自己制作su
    华南运维论坛 2015-07-25
    POJ2417 Baby-Step-Gaint-Step 算法
    《网络编程》套接字地址与名字转换
    Hibernate缓存
  • 原文地址:https://www.cnblogs.com/blogofwyl/p/4307499.html
Copyright © 2011-2022 走看看