C#从零开始学习(面向对象)(3)

面向对象

本章所有的代码都放在
https://github.com/hikinazimi/head-first-Csharp

  • 类包含方法,方法中包含完成动作的语句,设计良好的类要合理的命名
  • 一些方法有一个返回类型,要使用return返回对应的类型
  • 以public void为开头,可以不用return
  • 从一个类创建一个新的对象时,称为这个类的一个实例
  • 使用static关键字声明类或者方法声明为静态,访问静态方法或字段不需要创建这个类的一个实列
  • 如果一个字段是静态的,他只有一个副本,由所有实例共享

PickRandomCards选牌程序

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

namespace PickRandomCards
{
    class CardPicker
    {
        static Random random = new Random();
        public static string[] PickSomeCards(int numerOfCards)
        {
            string[] pickedCards = new string[numerOfCards];
            for (int i = 0; i < numerOfCards; i++)
            {
                pickedCards[i] = RandomValue() + " of " + RandomSuit();
            }
            return pickedCards;
        }

        private static string RandomValue()
        {
            //1-4的随机数
            int value = random.Next(1, 5);

            if (value == 1) return "Spades";
            if (value == 2) return "Hearts";
            if (value == 3) return "Culbs";
            return "Diamonds";
        }

        private static string RandomSuit()
        {
            int value = random.Next(1, 14);

            if (value == 1) return "Ace";
            if (value == 11) return "Jack";
            if (value == 12) return "Queen";
            if (value == 13) return "King";
            return value.ToString();
        }
    }
}

选牌为原型的WPF应用(PickACardUI)

将上面创建的游戏使用WPF构建

首先创建一个名为PickACardUI的WPF项目

然后在项目中添加上个项目中MainWindow.xaml.cs文件,鼠标右键项目名,然后添加文件

然后在文件中更改命名空间为项目名

使用Grid和StackPanel建立主窗口布局

使用StackPanel堆叠两个控件,使他们在同一个单元格中

xml 复制代码
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="20">
            
            <Label Content="How many cards should I pick?" FontSize="20"/>
            <Slider x:Name="numberOfCards" Minimum="1" Maximum="15" TickPlacement="BottomRight"
                IsSnapToTickEnabled="True" AutoToolTipPlacement="TopLeft" Foreground="Black"/>
        </StackPanel>

然后在按钮上定义点击的效果

csharp 复制代码
<Button Grid.Row="1" Content="Pick some cards" FontSize="20" 
            HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click" />

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string[] pickedCards = CardPicker.PickSomeCards((int)numberOfCards.Value);
            listOfCards.Items.Clear();
            foreach (string card in pickedCards)
            {
                listOfCards.Items.Add(card);
            }
        }

最终结果如下

JoeAndBob

创建一个类来跟踪他们分别有多少钱

  1. 创建两个"Guy"实例
  2. 设置对象的Cash和Name
  3. 增加方法来给钱和收钱
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

namespace JoeAndBob
{
   class Guy
   {
       public string Name;
       public int Cash;

       public void WriteMyInfo()
       {
           Console.WriteLine(Name + " has " + Cash + " bucks.");
       }

       public int GiveCash(int amount)
       {
           if (amount <= 0)
           {
               Console.WriteLine(Name + "says: " + amount + "isn't a vaild amount");
               return 0;
           }
           if (amount > Cash)
           {
               Console.WriteLine(Name + " says: " + "I don't have the enough cash to give you" + Cash);
               return 0;
           }
           Cash -= amount;
           return amount;

       }

       public void ReceiveCash(int amount)
       {
           if (amount <= 0)
           {
               Console.WriteLine(Name + " says: " + amount + " isn't an amount I'll take");
           }
           else
           {
               Cash += amount;
           }
       }
   }
}

类的设计想法

  • 构建的程序需要解决一个问题
  • 思考程序会使用哪些实际实物
  • 为类和方法使用描述性的名字
  • 注意类之间的相似性

至此,我们就学习完了第三章,然后让我们复习一下本章讲了什么

  • 学习了C#中类的定义和应用
  • 学习了如何将一个简单的控制台项目改写成WPF应用
相关推荐
lazy H18 分钟前
从入门到日常开发,一篇文章掌握 Git 核心操作
git·后端·学习·github
EIP低代码平台8 小时前
EIP 低代码平台 - 角色维护
低代码·c#·权限·工作流·netcore
a11177612 小时前
2FA 验证码生成器(github登录验证 app)
笔记·学习
我的xiaodoujiao12 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
心平气和量大福大13 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
你想知道什么?14 小时前
线性回归-学习笔记
笔记·学习·线性回归
鱼毓屿御14 小时前
从「只会聊」到「边想边做」的跃迁
前端·学习·react.js
AOwhisky15 小时前
Python 学习笔记(第十五期)——运维自动化(下·后篇):堡垒机实战——paramiko高阶篇
运维·python·学习·云原生·自动化·运维开发
minglie117 小时前
zynq中linux系统的自动登录和免密登录
学习
-银雾鸢尾-17 小时前
C#中HashTable相关方法
开发语言·c#