C#-互动

 

互动1
编写一个设备类myDevice,该类仅包含一个string类型私有字段成员ip。
编写myDevice构造函数,使能够设置ip初始值(最好能检测ip正确性)
编写一个只写属性IP,使能够设置ip值(最好能检测ip正确性)。
编写一个公有实例方法getIp(),  能够读取ip
。
编写一个控制台应用,  测试上述类及成员。
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
           
            //Program r = new Program();
            myDevice r1 = new myDevice("202.194.119.110");//正确的ip地址
            Console.WriteLine( "IP地址1:{0}",r1.getIp());

            myDevice r2 = new myDevice("202.194.119.312");//错误的ip地址
            Console.WriteLine("IP地址2:{0}", r2.getIp());
            Console.ReadLine();

        }
    }
    class myDevice
    {
        private string ip;
        const string pattern = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";//正则表达式
        public myDevice(string s)//构造函数
        {
            if (Regex.IsMatch(s, pattern))
                ip = s;
            else
            {
                Console.WriteLine("IP地址初始化错误");
            }
        }

        public string MyProperty//属性
        {
            set
            {
                if (Regex.IsMatch(value, pattern))
                    ip = value;
                else
                    Console.WriteLine("IP地址设置错误");
            }
        }
        public string getIp()
        {
            return ip;
        }
    }
}
互动2
程序已给出形状类Shape的定义,是使用关键字abstract限定的抽象类。
public abstract class Shape{
      public abstract int Area();
}
编写两个以形状类作为基类的正方形子类和圆形子类,并求二者面积。
正方形和圆形面积公式如下:
area = side * side;  //正方形面积=边长*边长
area= PI * radius * radius; //圆形面积= PI*半径平方,其中PI为常数,此处可设为3
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication4
{
  
    class Program
    {
        static void Main(string[] args)
        {
            int s=1;
            int r=1;
            cycle C = new cycle();
            C.SetR(r);
            square S = new square();
            S.SetS(s);

            Console.WriteLine("正方形的面积:{0}",S.Area());
            Console.WriteLine("圆的面接:{0}",C.Area());
            Console.ReadLine();
        }
    }
    public abstract class Shape
    {
        public abstract int Area();
    }
    class cycle:Shape
    {
        int r,PI=3;
        public  void SetR(int r)
        {
            this.r = r;
        }
        public override int Area()//重写Area();
        {
            return PI* r * r;
        }
    }
    class square:Shape
    {
        int s;
        public void SetS(int s)
        {
            this.s = s;
        }
        public override int Area()
        {
            return s * s;
        }
    }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值