zoukankan      html  css  js  c++  java
  • C#面向对象复习概要

    1.面向对象:我们将具有统一行为和属性的对象抽象划分为类,通过类去创建对象。这种编程思想叫做面向对象的编程思想。

    2.属性:对象具有的属性

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 属性
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person person1 = new Person();
                person1.Name = "tangxuelong";
                Console.Write("我的名字是{0}",person1.Name);
                person1.Name = "zhangsan";
                Console.Write("我的名字是{0}", person1.Name);
                person1.Name = "lisi";
                Console.Write("我的名字是{0}", person1.Name);
            }
        }
        public class Person
        {
            //private字段和public属性
            private string name;
            public string Name {
                set {
                    if (value == "tangxuelong")
                    {
                        this.name = value;
                    }
                    else if (value == "zhangsan")
                    {
                        this.name = "sunwukong";
                    }
                    else {
                        return;
                    }
                }
                get { return name; }
            }
        }
    }
    

    3.继承:子类拥有父类的属性和行为

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 继承
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person person1 = new Person();
                person1.eat();
                person1.walk();
            }
        }
        class Animal
        {
            public void eat()
            {
                Console.WriteLine("eat!");
            }
        }
        class Person : Animal
        {
            public void walk()
            {
                Console.WriteLine("walk!");
            }
        }
    }
    

    4.静态成员和非静态成员

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 静态成员
    {
        class Program
        {
            public int 实例成员() {
                return 2;
            }
            public static int 静态成员() {
                return 1;
            }
            
            static void Main(string[] args)
            {
                //在静态函数成员中,访问实例成员需要实例化
                Program p = new Program();
                p.实例成员();
    
                //在静态函数成员中,访问静态成员不需要实例化可以直接访问
                静态成员();
            }
            void 实例函数() 
            {
                //在实例函数成员中,可以直接访问实例成员和静态成员
                实例成员();
                静态成员();
            }
        }
    
    }
    

      

  • 相关阅读:
    Spring Mvc和Mybatis的多数据库访问配置过程
    Git下解决冲突
    安装Git
    数据库优化
    Ubuntu版 微信
    ssh框架简介
    写代码的习惯
    CentOS 7 安装 docker 并搭建私有仓库
    IPv4地址分类及特征
    Xcode 7.0 Could not find developer disk image
  • 原文地址:https://www.cnblogs.com/tangt/p/4179686.html
Copyright © 2011-2022 走看看