| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:有关c#继承问题,如何修改以下程序
只看楼主 加入收藏
yy1459635633
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
有关c#继承问题,如何修改以下程序
添加语句,使得能够选择性输出信息。例如,输入book和mage,跳出不同的信息。这里是选择输出书的属性和出版社的属性


using System;
using System.Collections.Generic;
using System.Text;

namespace SX01
{
    class catalogue //基类
    {
        public string title;
        public string writer;
        public string date;
        public void print_V()
        {
            Console.WriteLine("标题是什么?");
            this.title = Console.ReadLine();
            Console.WriteLine("作者是谁?");
            this.writer = Console.ReadLine();
            Console.WriteLine("发布时间是多少?");
            this.date = Console.ReadLine();
        }
        public void display()
        {
            Console.WriteLine();
            Console.WriteLine("标题是{0}", this.title);
            Console.WriteLine("作者是{0}", this.writer);
            Console.WriteLine("发布日期是{0}", this.date);
        }
        class articles : catalogue
        {
            string name;
            string number;
            public void print_A()
            {
                base.print_V();
                Console.WriteLine("杂志名称是什么?");
                this.name = Console.ReadLine();
                Console.WriteLine("杂志刊号是什么?");
                this.number = Console.ReadLine();
            }
            public void displayskill()
            {
                base.display();
                Console.WriteLine();
                Console.WriteLine("名称是{0}", this.name);
                Console.WriteLine("刊号是{0}", this.number);
            }
        }
        class books : catalogue
        {
            string cbmc;
            string cbdz;
            public void print_B()
            {
                base.print_V();
                Console.WriteLine("出版社叫什么?");
                this.cbmc = Console.ReadLine();
                Console.WriteLine("出版地址是哪里?");
                this.cbdz = Console.ReadLine();
            }
            public void displayskilled()
            {
                base.display();
                Console.WriteLine();
                Console.WriteLine("出版社是{0}", this.cbmc);
                Console.WriteLine("出版地址是{0}", this.cbdz);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                articles art = new articles();
                art.print_A();
                art.displayskill();


                books bo = new books();
                bo.print_B();
                bo.displayskilled();
            }

        }


    }
}
搜索更多相关主题的帖子: 信息 class public 出版社 选择性 
2012-03-22 13:39
yy1459635633
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-22
收藏
得分:0 
好难
2012-03-22 13:45
jfei
Rank: 4
来 自:郑州
等 级:业余侠客
帖 子:92
专家分:268
注 册:2011-8-27
收藏
得分:10 
设置一个flag,就行啦。呵呵
收到的鲜花

虾米们!!!有意者加QQ 2434202652,2632939128联系我
2012-03-22 13:52
visit1452
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2012-3-22
收藏
得分:10 
using System;
using System.Collections.Generic;
using System.Text;

namespace SX06
{
     class catalogue
    {
       public string biaoti;
       public string  zuozhe;
       public int riqi;
   
       public void can()
       {
          Console.WriteLine("标题");
          biaoti = Console.ReadLine();
          Console.WriteLine("作者");
          zuozhe = Console.ReadLine();
          Console.WriteLine("日期");
          riqi = int.Parse(Console.ReadLine());
     
        }  
         
            public void print_V()  //方法,输出相应的信息
        {
            Console.WriteLine("标题={0},作者={1},日期={2}", biaoti, zuozhe, riqi);

        }

       }  
      
   

     class  article:catalogue
      {
         private string  mingcheng;
         private string qikanhao;
        public void can1()
        {
            Console.WriteLine("杂志名称");
            mingcheng = Console.ReadLine();
            Console.WriteLine("期刊号");
            qikanhao = Console.ReadLine();
        }
      public void print_C()   
        {
           
                  //直接继承基类的方法
            Console.WriteLine("杂志名称={0},期刊号={1}", mingcheng, qikanhao);
        }

      }

    class books : catalogue
      {
           private string  chubanshe;
        private string chubandi;
           
        public void can2()
         {
             Console.WriteLine("出版社的名称");
             chubanshe= Console.ReadLine();
             Console.WriteLine("出版地");
             chubandi = Console.ReadLine();
         }
       public void print_T()   
        {

            
            Console.WriteLine("出版社的名称={0},出版地={1}", chubanshe, chubandi);
        }
      
      }

    class Program
    {
        static void Main(string[] args)
        {
            catalogue ca = new catalogue();
            article ar = new article();
            books bk = new books();
            ca.can();
            ar.can1();
            bk.can2();

            ca.print_V();
            ar.print_C();
            bk.print_T();
        }
    }
}  
         
2012-03-22 13:53
yy1459635633
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-22
收藏
得分:0 
在哪边设置一个flag??
2012-03-22 13:53
yy1459635633
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-22
收藏
得分:0 
回复 4楼 visit1452
不要乱发。。这个。
2012-03-22 13:54
yy1459635633
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-22
收藏
得分:0 
flag,如何在主函数中添加呢?请大神赐教
2012-03-22 14:04
yy1459635633
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-22
收藏
得分:0 
我将主函数改成这样,当输入articles时,出现错误了。为什么 ?输入books时却没有。。

                string Flag;
                Console.WriteLine("请输入books或者articles");
                Flag = Console.ReadLine();
                if  (Flag == "articles")
                {
                    articles art = new articles();
                    art.print_A();
                    art.displayskill();
                }
                if  (Flag == "books")
                {
                    books bo = new books();
                    bo.print_B();
                    bo.displayskilled();
                }
2012-03-22 14:20
快速回复:有关c#继承问题,如何修改以下程序
数据加载中...
 
   



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

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