C# class示例 0012

Defining a Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoxApplication
{
    class Box
    {
        public double length;
        public double width;
        public double height;
    }
    class Boxtester
    {
        static void Main(string[] args)
        {
            Box Box1 = new Box();
            Box Box2 = new Box();
            double volume = 0.0;

            Box1.height = 5.0;
            Box1.width = 4.0;
            Box1.length = 3.0;
            Box2.height = 3.0;
            Box2.width = 5.0;
            Box2.length= 4.0;

            // volumne of Box1
            volume = Box1.height * Box1.width * Box1.length;
            Console.WriteLine("volume of Box1 {0}", volume);

            // volumne of Box2
            volume = Box2.height * Box2.width * Box2.length;
            Console.WriteLine("volume of Box2 {0}", volume);
            Console.ReadKey();
        }
    }
    }

output:
volume of Box1 60
volume of Box2 60


Member Functions and Encapsulation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoxApplication
{
    class Box
    {
        public double length;
        public double width;
        public double height;
        public void setLength(double len)
        {
            length = len;
        }
        public void setWidth(double wid)
        {
            width = wid;
        }
        public void setHeight(double hei)
        {
            height = hei;
        }

public double getVolume()
        {
            return height * length * width;
        }

    }
    class Boxtester
    {
        static void Main(string[] args)
        {
            Box Box1 = new Box();
            Box Box2 = new Box();
            double volume;

            Box1.setLength(6.0);
            Box1.setHeight(4.0);
            Box1.setWidth(3.0);
            // Box2 specification 
            Box2.setLength(12.0);
            Box2.setHeight(14.0);
            Box2.setWidth(13.0);


            // volumne of Box1
            volume = Box1.getVolume();
            Console.WriteLine("volume of Box1 {0}", volume);

            // volumne of Box2
            volume = Box2.getVolume();
            Console.WriteLine("volume of Box2 {0}", volume);
            Console.ReadKey();



        }

    }

    }

output:
volume of Box1 72
volume of Box2 2184

C# Constructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LineApplication
{
    class Line
    {
        private double length; // Length of Line
        public Line() { Console.WriteLine("Object is being crated"); }
        public void setLength(double len)
        {
            length = len;
        }
        public double getLength()
        {
            return length;
        }
        static void Main(string[] args)
        {
            Line l = new Line();
            // set line length
            l.setLength(6.0);
            Console.WriteLine("Length of line : {0}", l.getLength());
            Console.ReadKey();

        }
    }
}

output:
Object is being crated
Length of line : 6


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LineApplication
{
    class Line
    {
        private double length; // Length of Line
        public Line( double  len) // Parameterized constructor
        { 
            Console.WriteLine("Object is being crated",len);
            length = len;
        }

        public void setLength(double len)
        {
            length = len;
        }
        public double getLength()
        {
            return length;
        }
        static void Main(string[] args)
        {
            Line line = new Line(10.0);
            Console.WriteLine("Length of line : {0}", line.getLength());
            // set line length
            line.setLength(6.0);
            Console.WriteLine("Length of line : {0}", line.getLength());
            Console.ReadKey();

        }
    }
}

output:
Object is being crated
Length of line : 10
Length of line : 6


C# Destructors

Destructor can be very useful for releasing memory resources before exiting the program. Destructors cannot be inherited or overloaded.

using System;
namespace LineApplication
{
    class Line
    {
        private double length; // Length of Line
        public Line( ) //  constructor
        { 
            Console.WriteLine("Object is being crated");
        }
        ~ Line() { // destructor
            Console.WriteLine("Object is being deleted");
        } 

        public void setLength(double len)
        {
            length = len;
        }
        public double getLength()
        {
            return length;
        }
        static void Main(string[] args)
        {
            Line line = new Line();
            // set line length
            line.setLength(6.0);
            Console.WriteLine("Length of line : {0}", line.getLength());
            Console.ReadKey();
        }
    }
}

output:
Object is being crated
Length of line : 6

  • why destructor not excuted?

Static Member of a C# Class

using System;
namespace StaticVarApplication
{
    class staticVar
    {
        public static int num;
        public void count()
        {
            num++;
        }
        public int getNum()
        {
            return num;
        }
        class StaticTester
        {
        static void Main(string[] args)
        {
                staticVar s1 = new staticVar();
                staticVar s2 = new staticVar();
                s1.count();
                s1.count();
                s1.count();
                s2.count();
                s2.count();
                s2.count();

                Console.WriteLine("Varible num for s1:{0}", s1.getNum());
                Console.WriteLine("Varible num for s2:{0}", s2.getNum());
                Console.ReadKey();


        }

        }
   }
}

output:
Varible num for s1:6
Varible num for s2:6

when I write wrong the
Console.WriteLine(“Varible num for s2:{ 0}”, s2.getNum());
It’s occured An unhandled exception of type ‘System.FormatException’ occurred in mscorlib.dll

static Method

using System;
namespace StaticVarApplication
{
    class staticVar
    {
        public static int num;
        public void count()
        {
            num++;
        }
        public static  int getNum()
        {
            return num;
        }
        class StaticTester
        {
        static void Main(string[] args)
        {
                staticVar s1 = new staticVar();
                staticVar s2 = new staticVar();
                s1.count();
                s2.count();
                Console.WriteLine("Varible num for s1:{0}", staticVar.getNum());
                Console.WriteLine("Varible num for s2:{0}", staticVar.getNum());
                Console.ReadKey();


        }

        }
   }
}

output:
Varible num for s1:2
Varible num for s2:2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值