标题:关于new对象的问题
取消只看楼主
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
 问题点数:0 回复次数:7 
关于new对象的问题
using System;
using System.Collections.Generic;
using System.Text;

namespace DataStructure
{
    ///
    /// Class1 的摘要说明。
    ///
    public class Stack//栈类
    {
        //private int count = 0;
        //private Node first = null;//定义首结点
        public bool Empty
        {
            get
            {
                return (first == null);
            }
        }

      

        public int Count
        {
            get
            {
                return count;
            }
        }
        public object Pop()//出栈
        {
            if (first == null)
            {
                throw new InvalidOperationException("不能从一个空栈出栈!");
            }
            else
            {
                object temp = first.Value;
                first = first.Next;
                count--;
                return temp;
            }
        }
        //这里跟方法签名里的参数类型不同也可以吗?
        public void push(object o)//入栈
        {
            first = new Node(o, first);
            count++;
        }
        public Stack()
        {
            System.Console.WriteLine("建立了一个空栈");
            
        }
        private int count = 0;
        private Node first = null;//定义首结点


        public void Print()
        {
            
            object temp = Pop();
            //first = Pop();
            System.Console.Write("{0}   ",temp.ToString());
        }


    static void Main(string[] args)
    {
        
        int n;
        System.Console.Write("请输入入栈的元素个数:");
        n = int.Parse(System.Console.ReadLine());
        Stack stk = new Stack();

        //这个for循环不明白,一直创建对象,又没有数组将值放在一起,如何可以输出这么多对象的值呢?
        for ( int i = 0; i < n; i++)
        {
            stk.push(i);
        }
        while (stk.first != null)
            {
                stk.Print();
            }
            Console.ReadLine();
    }
    }
    class Node //结点类
    {
        public Node Next;
        public object Value;
        public Node(object value) : this(value, null) { }
        public Node(object value, Node next)
        {
            Next = next;                          
            Value = value;
        }
    }

   
}
搜索更多相关主题的帖子: new 对象 get using public 
2008-04-09 22:28
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
问题是为什么main函数里面Stack stk = new Stack(); 这个对象会执行
private int count = 0;
private Node first = null;//定义首结点
后。。。才雕用构造函数
 public Stack()
        {
            System.Console.WriteLine("建立了一个空栈");
            
        }
2008-04-09 22:31
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
与上面对比   一个不调用字段  直接调用构造函数的例子
using System;
using System.Collections.Generic;
using System.Text;

namespace third2
{
      class Animal
    {
         bool mammal;
         bool carnivorous;
         int mood;

        public int Mood
        {
            get { return mood; }
            set { mood = value; }
        }
        public  bool IsMammal(bool mammalval)
        {
            if (mammalval == true)
            {
                System.Console.WriteLine("哺乳类动物!");
            }
            else
            {
                System.Console.WriteLine("不是哺乳类动物!");
            }
            mammal = mammalval;
            return mammal;
        }
        public  bool IsCarnivorous(bool carnivorousval)
 
        {
            if (carnivorousval == true)
            {
                System.Console.WriteLine("食肉类动物!");
            }
            else
            {
                System.Console.WriteLine("不是食肉类动物!");
            }
            carnivorous = carnivorousval;
            return carnivorous;
        }
          public virtual void SayHello()
       {  }
        public virtual void SayHello(int moodval)
        { }


    }
}
2008-04-09 22:32
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
using System;
using System.Collections.Generic;
using System.Text;

namespace third2
{
    class Dog : ILandAnimal
    {
        bool mammal;
        bool carnivorous;
        int numberOfLegs;
        public override int GetNumberOfLegs(int numberOfLegVal)
        {
            if (numberOfLegVal < 0 || numberOfLegVal > 4)
            {
                System.Console.WriteLine("输入不正确那是狗吗?");
                return 0;
            }
            else
            {   
                numberOfLegs = numberOfLegVal;
                System.Console.WriteLine("狗的腿子有:{0}个 哈哈!",numberOfLegs);  
                return numberOfLegs;
            }
        }
        public override void SayHello()
        {
            System.Console.WriteLine("摇 尾 巴!");
        }
        public Dog()
        {
            System.Console.WriteLine("狗的属性如下:");
            mammal = IsMammal(true);
            carnivorous = IsCarnivorous(true);
            SayHello();

        }
        public override void SayHello(int moodval)
        {
            if (moodval == 1)
            {
                System.Console.Write("高兴的时候:");
                System.Console.WriteLine("汪  汪  汪!");
            }
            else
            {
                if (moodval == 0)
                {
                    System.Console.Write("不高兴的时候:");
                    System.Console.WriteLine("呜呜叫!");
                }
                System.Console.WriteLine("一般时候!");
            }
            
        }



    }
}
2008-04-09 22:32
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
using System;
using System.Collections.Generic;
using System.Text;

namespace third2
{
    class Program
    {
        static void Main(string[] args)
        {
            int mood;
            Dog dog = new Dog();
            dog.GetNumberOfLegs(4);
            System.Console.Write("请输入狗的心情 1 / 好 ,0 / 郁闷");
            mood = int.Parse(System.Console.ReadLine());
            dog.SayHello(mood);

            Cat cat = new Cat();
            cat.GetNumberOfLegs(4);
            System.Console.Write("请输入猫的心情 1 / 好 ,0 / 郁闷");
            mood = int.Parse(System.Console.ReadLine());
            cat.SayHello(mood);

            Frog frog = new Frog();
            frog.GetNumberOfLegs(4);
            System.Console.Write("请输入青蛙的心情 1 / 好 ,0 / 郁闷");
            mood = int.Parse(System.Console.ReadLine());
            frog.SayHello(mood);
            Console.ReadLine();

        }
    }
}
2008-04-09 22:33
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
using System;
using System.Collections.Generic;
using System.Text;

namespace third2
{
    class ILandAnimal:Animal
    {
        int legs;
        public virtual int GetNumberOfLegs(int legsval)
        {
            legs = legsval;
            return legs;
        }
    }
}
2008-04-09 22:34
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
using System;
using System.Collections.Generic;
using System.Text;

namespace third2
{
    class IWaterAnimal : Animal
    {
        bool gills;
        bool lays;
        public virtual  bool HasGills(bool gillsval)
        {
            gills = gillsval;
            return gills;
        }
        public bool LaysEggs(bool laysval)
        {
            if (laysval)
            {
                System.Console.WriteLine("卵生的!");
            }
            else
            {
                System.Console.WriteLine("不是卵生的!");
            }

            lays = laysval;
            return lays;
        }
   
    }
}
2008-04-09 22:34
足迹
Rank: 1
来 自:广东惠州
等 级:新手上路
帖 子:46
专家分:0
注 册:2008-4-2
得分:0 
这个例子中  main函数
static void Main(string[] args)
        {
            int mood;
            Dog dog = new Dog();
            dog.GetNumberOfLegs(4);
            System.Console.Write("请输入狗的心情 1 / 好 ,0 / 郁闷");
            mood = int.Parse(System.Console.ReadLine());
            dog.SayHello(mood);
调用Dog dog = new Dog();时  他是不会执行类class Dog : ILandAnimal
里面的字段bool mammal;
        bool carnivorous;
        int numberOfLegs;而直接执行构造函数的
public Dog()
        {
            System.Console.WriteLine("狗的属性如下:");
            mammal = IsMammal(true);
            carnivorous = IsCarnivorous(true);
            SayHello();

        }
2008-04-09 22:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208099-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.365380 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved