天天看点

C#基础07

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base07
{
    class Program
    {
        static void Main(string[] args)
        {
            //对象 万事万物皆为对象
            //基本的特征 类
            //特征=属性  动作=方法
            //需要使用到类中的属性 1 实例化类对象 2 用类对象的别名来进行属性的赋值
            //Person p = new Person();
            //Person p = new Person("zs", 1);
            //Person p = new Person("zs");
            // p._pname = "zs";
            //p.pid = 1;
            //p.ShowPname();
            //Console.WriteLine(p.getPname());
            //Console.WriteLine(p.getPname("1234"));
            //p.ShowPanames();
            // Console.WriteLine("{0}:{1}",p.pname,p.pid);

            //值类型  引用类型
            //引用类型 new 堆
            //值类型 (基础的数据结构,类型) 无new 栈

            //结构
            //Person person;
            //person.pid = 1;
            //Console.WriteLine(person.pid);
            //Console.ReadKey();

        }
    }
}

--------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base07
{
    struct Persons
    {
        public string pname;
        public int pid;
    }
}
---------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base07
{
    //访问修饰符
    //公共  私有  受保护  默认的修饰符
    //修饰符访问的范围
    //公共  所有都可以进行访问
    //私有  当前的类中可以被访问
    //受保护  子类可访问
    //默认的修饰符  当前的类库中可以被访问

   class Person
    {
        //构造函数  1无参(无参) 2有参 构造函数可以重载
       /// <summary>
       /// 无参的构造函数
       /// </summary>
        //public Person()
        //{
        //    Console.WriteLine("无参的构造方法1");
        //}
        //public Person(string pname,int pid)
        //{
        //    this._pname = pname;
        //    this._pid = pid;
        //    Console.WriteLine("带参数的构造函数");
        //}

        //public Person(string pname)
        //{
        //    this._pname = pname;
        //    this._pid = 2;
        //    Console.WriteLine("带参数的构造函数重载");
        //}
        / <summary>
        / 释放资源
        / </summary>
        //~Person(){
        //    this._pname = null;
        //    this._pid = 0;
        //    Console.WriteLine("析构函数");
        //}
        //public string _pname;
        //public int _pid;

        方法 四类
        同名不同参 与返回值无关 方法的重载
        1 无参无返回 
        //public void ShowPname()
        //{
        //    Console.WriteLine(_pname);
        //}
        2 有参无返回
        //public void SHowpname(string pname)
        //{
        //    if (pname.Length > 3)
        //    {
        //        Console.WriteLine(pname);
        //    }else
        //    {
        //        Console.WriteLine(this._pname);
        //    }
        //}
        3 无参有返回
        返回值的类型 有方法定义的数据类型来决定
        //public string getPname()
        //{
        //    if (_pname.Length > 3)
        //    {
        //        return _pname;
        //    }
        //    return "默认值";
        //}

        4 有参有返回
        //public string getPname(string pname)
        //{
        //    if (_pname.Length > 3)
        //    {
        //         _pname=pname;
        //    }
        //    return _pname;
        //}

        //public void ShowPanames()
        //{
        //    Console.WriteLine(getPname("1234"));
        //}

    }
}

           

写一个狗的类,类中有属性:姓名,性别,品种。(使用3个构造方法进行对象的初始化)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Person
{
    class Dog
    {
   
        //public string _name;
        //public string _sex;
        //public string _type;

          (一)无返回,无参数的方法:
         狗跑步的方法,要求输出“某某品种的狗跑得好快”。
        //public void run()
        //{
        //    Console.WriteLine(_type + "的狗跑得好快");
        //}
         狗长大的方法,要求输出“某某姓名的狗长大了”。
        //public void grow()
        //{
        //    Console.WriteLine("姓名:" + _name + "的狗长大了");
        //}

          (二)有返回,无参数的方法:
         得到狗的姓名方法,要求返回当前狗的姓名。
        //public string Name()
        //{
        //    Console.WriteLine("狗的姓名:"+_name);
        //    return _name;
        //}
         得到狗的性别的方法,要求返回当前狗的性别。
        //public string Sex()
        //{
        //    Console.WriteLine("狗的性别:" + _sex);
        //    return _sex;
        //}
         得到狗的品种的方法,要求返回当前狗的品种。
        //public string Type()
        //{
        //    Console.WriteLine("狗的品种" + _type);
        //    return _type;
        //}

         返回当前对象信息的方法,即toString()方法。
        //public string toString()
        //{
        //    //Console.WriteLine("姓名:{0},性别:{1},品种:{2}",_name,_sex,_type);
        //    return "姓名:" + _name +","+ "性别:" + _sex +","+ "品种:"+","+  _type;
        //}

          (三)无返回,有参数的方法:
         设置当前狗的姓名的方法,要求传入狗的新姓名,修改当前姓名。
        //public void setName(string name)
        //{
        //    _name = name;
        //}

         设置当前狗的性别的方法,要求传入狗的新性别,修改当前性别。
        //public void setSex(string sex)
        //{
        //    _sex = sex;
        //}

         设置当前狗的品种的方法,要求传入狗的新品种,修改当前品种。
        //public void setType(string type)
        //{
        //    _type = type;
        //}

    }
}

           

写一个测试类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Person
{
    class Program
    {
        static void Main(string[] args)
        {
            //     写一个测试类,要求首先生成一只狗,使用设置方法将狗的属性设上值,再调用狗跑步的方法,
            // 调用狗长大的方法。再调用得到狗姓名的方法,接收返回,并输出返回的姓名。
            // 最后使用输出,将这只狗的所有信息输出。

            //Dog d = new Dog();
            //d._name = "好好";
            //d._sex = "公";
            //d._sex = "母";
            //d._type = "土狗";
                        
            //d.run();
            //d.grow();         
            //Console.WriteLine(d.Name());
            //Console.WriteLine(d.toString());
            //Console.ReadKey();
        }
    }
}