C#面向对象基础(一) 类与对象 欢乐农场篇

面向对象 OO   Object-Oriented

面向对象程序设计的思路与传统面向过程是不同地.

面向对象方法考虑的是类,让你很累的"类".

面向对象认为,程序是由对象组成. 程序运行开始,制作一大堆的对象,对象挤对对象,对象内有对象,对象之间交互...  这就是程序.

为什么要面向对象?便于理解,更接近我们的思维方式.

类与对象的关系:类是对象的图纸...  类是对象的模子,中秋快到了,  见过月饼制作木有? 什么样的模子,就有什么样的月饼...

参考农场: 造一个农场先,家场是空的?快,造一些动物出来,牛羊鸡兔...

第一版,

动物,应大家的要求设计

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

namespace ConApp
{
    
/// <summary>
    
/// 动物(农场里的)
    
/// 名字 年龄  饿否? 位置(x,y) 色  生产 价格
    
/// </summary>
    public class Animal
    {
        
// 字段  存储这些数据
        public string name;
        
public int age;
        
public bool ishungry;
        
public int x;
        
public int y;
        
public int count; //生产
        public float price;
    }
}

农场里只有四个小动物,有构造

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

namespace ConApp
{
    
/// <summary>
    
/// 农场
    
/// </summary>
    public  class Farm
    {
        
public Animal cock;
        
public Animal goat;
        
public Animal cow;
        
public Animal rabbit;

        
/// <summary>
        
/// 构造方法  开始建农场了  初始化
        
/// </summary>
        public Farm()
        {
            Console.WriteLine(
"农场开始构造   building....");
            cock 
= new Animal();
            goat 
= new Animal();
            cow 
= new Animal();
            rabbit 
= new Animal();

            cock.name 
= "cock";
            cock.price 
=2;
            cock.ishungry 
= true;
            cock.age 
= 1;
            cock.x 
= 3;
            cock.y 
= 3;

            goat.name 
= "goat";
            goat.price 
= 3;
            goat.ishungry 
= false;
            goat.age 
= 3;
            goat.x 
= 5;
            goat.y 
= 5;

            Console.WriteLine(
"农场初始化成功");
        }
    }
}
原文地址:https://www.cnblogs.com/imxh/p/2168378.html