| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 771 人关注过本帖
标题:请教老师大师们:基类,派生类的用法和窗体的输出
只看楼主 加入收藏
wudics
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-12-27
收藏
 问题点数:0 回复次数:2 
请教老师大师们:基类,派生类的用法和窗体的输出
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

         
        public class auto
        {
            public int _wheels; //车轮
            public double _weight; //车重
            public auto(int wheels, double weight)
            {
                _wheels = wheels ;
                _weight = weight ;
            }
            int getwheels()
            {
                return _wheels ;
            }
            double getweight()
            {
                return _weight ;
            }
        }

         class car : auto
         {
             public car(int wheels, double weight)
                 : base(wheels, weight)
             {
             }
         }
        
        class truck: auto
        {
            public truck(int wheels, double weight)
                : base(wheels, weight)
            {            
            }

        }

        class bike: auto
        {
             public bike(int wheels, double weight)
                : base(wheels, weight)
            {
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "轿车")
            {
                new car(4, 1000);
                textBox1.Text = "轿车" ;
                textBox2.Text = "4";
                textBox3.Text = "1000";
            }
            else if (comboBox1.Text == "卡车")
            {
                new truck(12, 6000);
                textBox1.Text = "卡车";
                textBox2.Text = "12";
                textBox3.Text = "6000";
            }
            else if (comboBox1.Text == "自行车")
            {
                new bike(2, 60);
                textBox1.Text = "自行车";
                textBox2.Text = "2";
                textBox3.Text = "60";
            }
            else MessageBox.Show ("请选择你要查询的项目。");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
  
    }
}


这是我的课题要求
设计一个汽车类族
要求:一个基类AUTO;
若干个派生类分别描述轿车、卡车、自行车等等。
设计相应Windows界面用于选择性地输出各种信息。

对于赋于派生类的值怎么在textbox中输出了?
要是和汉字一起输出,即把3个textbox合并成一个.不知道如何是好...
搜索更多相关主题的帖子: class public double wheels 
2011-12-27 12:01
wangzhen_andy
Rank: 2
来 自:广州
等 级:论坛游民
帖 子:39
专家分:25
注 册:2008-9-22
收藏
得分:0 
基类中写一个抽象方法:public abstract OutputAutoInfo();
然后在每一个派生类中重载这个方法,方法中就是将子类中的属性输出到textbox。

使用的时候,有两种方法:
1. switch语句,根据输入的文字名称,去实例化对象:
Auto obj = New car();
Auto obj = New Bike();
...
输入就使用 obj.OutPutAutoInfo()即可。
2. 使用反射的方法,根据输入的名称,找到需要实例化的类型,进行实例化。比较复杂,这种简单程序不建议使用。
2012-01-17 20:46
guming
Rank: 4
等 级:业余侠客
威 望:5
帖 子:329
专家分:277
注 册:2006-11-9
收藏
得分:0 
private void button1_Click(object sender, EventArgs e)
        {
          if (comboBox1.Text == "轿车")
            {
               car car= new car(4, 1000);
                textBox1.Text = "轿车" ;
                textBox2.Text = car._wheels;
                textBox3.Text = car._weight;
                 //三个一起输出
                textBox1.Text = "轿车"+ car._wheels+car._weight;
            }
            else if (comboBox1.Text == "卡车")
            {
               truck truck= new truck(12, 6000);
                textBox1.Text = "卡车";
                textBox2.Text = truck._wheels;
                textBox3.Text = truck._weight;
            }
            else if (comboBox1.Text == "自行车")
            {
               bike bike= new bike(2, 60);
                textBox1.Text = "自行车";
                textBox2.Text =bike._wheels;
                textBox3.Text =bike._weight;
            }
            else MessageBox.Show ("请选择你要查询的项目。");
        }

不想停留。。。
2012-01-21 01:00
快速回复:请教老师大师们:基类,派生类的用法和窗体的输出
数据加载中...
 
   



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

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