标题:构造函数和方法的问题求解
只看楼主
qyw000
Rank: 2
等 级:论坛游民
帖 子:28
专家分:36
注 册:2010-2-5
结帖率:80%
已结贴  问题点数:8 回复次数:5 
构造函数和方法的问题求解
输入的年龄如果小于等于0或者大于100,那么年龄就默认是18
在USER类里怎么写属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace User_OOP
{
    class UserHelp
    {
        //用户名
        private char userID;

        public char UserID
        {
            get { return userID; }
            set { userID = value; }
        }
        //密码
        private char password;

        public char Password
        {
            get { return password; }
            set { password = value; }
        }
        //年龄
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        //性别
        private char sex;

        public char Sex
        {
            get { return sex; }
            set { sex = value; }
        }
    }
}
搜索更多相关主题的帖子: 问题求解 函数 构造 
2010-05-15 12:44
xueshui20
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:269
专家分:309
注 册:2009-4-19
得分:2 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace age
{
    class User
    {
        public int age;
        public void SetAge(int v)//设置属性
        {
            if (v < 0 || v > 100)
            {
                age = 18;
            }
            else
                age = v;
        }

    }
    class Program
    {
      
        static void Main(string[] args)
        {
            User us = new User();
            us.SetAge(int.Parse(Console.ReadLine()));//调用属性
            Console.WriteLine(us.age);
            Console.ReadLine();
        }
    }
}
2010-05-15 13:37
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:2 
程序代码:
public int Age
        {
            get { return age; }
            set {
                 if (value <=0 || value >100)
                        age=8;
                 else
                        age = value; }
        }
2010-05-15 15:51
qyw000
Rank: 2
等 级:论坛游民
帖 子:28
专家分:36
注 册:2010-2-5
得分:0 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data.SqlClient;

namespace User_OOP
{
    class UserHelp
    {
        //用户名
        private string userID;

        public string UserID
        {
            get { return userID; }
            set { userID = value; }
        }
        //密码
        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        //确认密码
        private char passtoo;

        public char Passtoo
        {
            get { return passtoo; }
            set { passtoo = value; }
        }
        //年龄
        private int age;

        public int Age
        {
            get { return age; }
            set
            {
                if (age <= 0 || age >= 100)
                {
                    age = 18;
                }
                else
                {
                    age = value;
                }
            }
        }
        //性别
        private string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        public UserHelp(string name, string pass)
        {
            this.UserID = name;
            this.Password = pass;

        }
        public UserHelp(string name, string pass, int age, string sex)
        {
            this.UserID = name.ToString();
            this.Password = pass.ToString();

            this.Age = Convert.ToInt32(age);
            this.Sex = sex.ToString();

        }

        public string Show(string message)
        {
            string sql = string.Format("insert into dbo.UserInfo(userID,pass,age,sex)values('{0}','{1}','{2}','{3}')", userID, password, age, sex);
            SqlCommand comm = new SqlCommand(sql, dbhelper.conn);
            dbhelper.conn.Open();
            int count = Convert.ToInt32(comm.ExecuteNonQuery());
            if (count>0)
            {
                message = string.Format(userID, password, age, sex);
                return message;
            }
            return message;
        }



    }
}
USE 类中 当我注册成功时候 MESSAGEBOX消息框弹出我注册信息
求解

在MAIN方法下怎么写,因为在类里没有MESSAGEBOX
2010-05-15 16:43
封闭
Rank: 9Rank: 9Rank: 9
来 自:广东省汕头市
等 级:蜘蛛侠
威 望:1
帖 子:501
专家分:1084
注 册:2007-9-14
得分:2 
可以导入命名空间:using System.Windows.Form;
2010-05-15 18:06
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
得分:2 
以下是引用apull在2010-5-15 15:51:44的发言:

public int Age
        {
            get { return age; }
            set {
                 if (value <=0 || value >100)
                        age=8;
                 else
                        age = value; }
        }
同意3楼版主,只是要把age=8;改为age=18;
2010-05-17 20:39



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




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

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