十、面相对象

课程链接:

https://www.bilibili.com/video/BV1NA4y1R7vL?p=23&vd_source=0f636f173a0a5855cca528319972bfe9

建议学完本节后再补充一下static相关知识。

面向对象

在实际操作过程中,可以通过创建一个类来定义某种具有共同特征的对象,比如游戏角色中的英雄(Hero)。

class Hero{
  string name;//在不赋予初始值时,系统会默认给一个初始值。
  string gender;
  int age;
  string skill = "如来神掌";
  int skillHurt = 20;
}

可以定义不同的实例来访问类中的变量。

Hero hr = new Hero();//实例化:在hero类下创建一个名为hr的新实例。

要在外部访问hero的属性,需要将hero中定义的变量加上pulic修饰符。

class Hero{
  public string name;//在不赋予初始值时,系统会默认给一个初始值。
  public string gender;
  public int age;
  public string skill = "如来神掌";
  public int skillHurt = 20;
}

此时可以在hero类代码块之外进行引用

Hero hr = new Hero();//实例化:在hero类下创建一个名为hr的新实例。

hr.name = "zero";
hr.gender = "男";
hr.age = 21;
hr.skill = "如来神掌";
hr.skillHurt = 20;

class Hero{
  public string name;//在不赋予初始值时,系统会默认给一个初始值。
  public string gender;
  public int age;
  public string skill = "如来神掌";
  public int skillHurt = 20;
}

除了属性外,class 还可以有方法(功能)。

class Hero{
  public string name;//在不赋予初始值时,系统会默认给一个初始值。
  public string gender;
  public int age;
  public string skill = "如来神掌";
  public int skillHurt = 20;

  Public void showInfo{
    Console.writeline("你的名字是:"+name);
    Console.writeline("你的性别是:"+gender);
    Console.writeline("你的年龄是:"+age);
    Console.writeline("你的技能是:"+skill);
    Console.writeline("你的技能伤害是:"+skillHurt);
  }
}

在类中定义的方法也可以在其他地方进行引用。

Hero hr = new hero();//实例化:在hero类下创建一个名为hr的新实例。

hr.name = "zero";
hr.gender = "男";
hr.age = 21;
hr.skill = "如来神掌";
hr.skillHurt = 20;
hr.showInfo();

class Hero{
  public string name;//在不赋予初始值时,系统会默认给一个初始值。
  public string gender;
  public int age;
  public string skill = "如来神掌";
  public int skillHurt = 20;

  public void showInfo(){
     Console.WriteLine("你的名字是:" + name);
     Console.WriteLine("你的性别是:" + gender);
     Console.WriteLine("你的年龄是:" + age);
     Console.WriteLine("你的技能是:" + skill);
     Console.WriteLine("你的技能伤害是:" + skillHurt);
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace mianxiangduixiang
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("正在为你创建角色");
            Hero hr = new Hero();//实例化:在hero类下创建一个名为hr的新实例。
            Console.WriteLine("请输入你的姓名");
            hr.name = Console.ReadLine();
            Console.WriteLine("请输入你的性别");
            hr.gender = Console.ReadLine();
            Console.WriteLine("请输入你的年龄");
            hr.ageIput = Console.ReadLine();
            hr.strToInt();
            hr.showInfo();

            Console.ReadKey();
        }
        class Hero
        {
            public string name;//在不赋予初始值时,系统会默认给一个初始值。
            public string gender;
            public string ageIput;
            public int age;
            public string skill = "如来神掌";
            public int skillHurt = 20;
            
            public void strToInt()
            {
                age = int.Parse(ageIput);
            }
            public void showInfo()
            {
                Console.WriteLine("你的名字是:" + name);
                Console.WriteLine("你的性别是:" + gender);
                Console.WriteLine("你的年龄是:" + age);
                Console.WriteLine("你的技能是:" + skill);
                Console.WriteLine("你的技能伤害是:" + skillHurt);
            }
        } 
    }
}

扩展

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace mianxiangduixiang
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("正在为你创建角色");
            Hero hr = new Hero();//实例化:在hero类下创建一个名为hr的新实例。
            Console.WriteLine("请输入你的姓名");
            hr.name = Console.ReadLine();
            Console.WriteLine("请输入你的性别");
            hr.gender = Console.ReadLine();
            Console.WriteLine("请输入你的年龄");
            hr.ageIput = Console.ReadLine();
            hr.strToInt();

            ShowInfo showHeroInfo = new ShowInfo();
            showHeroInfo.showInfo(hr);

            Hero hrWoman = new Hero();
            Console.WriteLine("请输入你的姓名");
            hrWoman.name = Console.ReadLine();
            Console.WriteLine("请输入你的性别");
            hrWoman.gender = Console.ReadLine();
            Console.WriteLine("请输入你的年龄");
            hrWoman.ageIput = Console.ReadLine();
            hrWoman.strToInt();
            showHeroInfo.showInfo(hrWoman);

            Console.ReadKey();
        }

        class ShowInfo
        {
            public void showInfo(Hero hero) //这里后面的hero可以是任意内容,只是一个参数。
            {
                Console.WriteLine("你的名字是:" + hero.name);
                Console.WriteLine("你的性别是:" + hero.gender);
                Console.WriteLine("你的年龄是:" + hero.age);
                Console.WriteLine("你的技能是:" + hero.skill);
                Console.WriteLine("你的技能伤害是:" + hero.skillHurt);
            }
        }
        class Hero
        {
            public string name;//在不赋予初始值时,系统会默认给一个初始值。
            public string gender;
            public string ageIput;
            public int age;
            public string skill = "如来神掌";
            public int skillHurt = 20;
            
            public void strToInt()
            {
                age = int.Parse(ageIput);
            }
        } 
    }
}

建议学完本节后再补充一下static相关知识。


封面图为AI生成

工具:Stable Diffusion

来源:公众号设计师深海

「中国山水」(Chinese landscape)「中国传统建筑」(Chinese Traditional architecture)

「东方僧人」(Oriental Monk)「禅意」(zen)

「Lotus Pond」荷花池

艺术家 Alessandro Gottardo 作品的构图,就是我想要的场景,如果我们只用它的名字作为关键词,能够输出这种效果:

艺术家 Stephen Gammell 作品中呈现的效果是这样的:

prompt:

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注