zoukankan      html  css  js  c++  java
  • C#和.Ne学习第十天

    1、命名空间
    可以认为类是属于命名空间的。
    如果在当前项目中没有这个类的命名空间,需要我们手动的导入这个类所在的
    命名空间。
    1)、用鼠标去点
    2)、alt+shift+F10
    3)、记住命名空间,手动的去引用

    2、在一个项目中引用另一个项目的类
    1)、添加引用
    2)、引用命名空间

    3、值类型和引用类型
    区别:
    1、值类型和引用类型在内存上存储的地方不一样。
    2、在传递值类型和传递引用类型的时候,传递的方式不一样。
    值类型我们称之为值传递,引用类型我们称之为引用传递。
    我们学的值类型和引用类型:
    值类型:int、double、bool、char、decimal、struct、enum
    引用类型:string、自定义类、数组
    存储:
    值类型的值是存储在内存的栈当中。
    引用类型的值是存储在内存的堆中。
    例图:1

    3、字符串
    1)、字符串的不可变性
    当你给一个字符串重新赋值之后,老值并没有销毁,而是重新开辟一块空间存储新值。

    当程序结束后,GC扫描整个内存,如果发现有的空间没有被指向,则立即把它销毁。


    2)、我们可以讲字符串看做是char类型的一个只读数组。
    ToCharArray();将字符串转换为char数组
    new string(char[] chs):能够将char数组转换为字符串

    4、字符串提供的各种方法
    1)、Length:获得当前字符串中字符的个数
    2)、ToUpper():将字符转换成大写形式
    3)、ToLower():将字符串转换成小写形式
    4)、Equals(lessonTwo,StringComparison.OrdinalIgnoreCase):比较两个字符串,可以忽略大小写
    5)、Split():分割字符串,返回字符串类型的数组。
    6)、Substring():解决字符串。在截取的时候包含要截取的那个位置。
    7)、IndexOf():判断某个字符串在字符串中第一次出现的位置,如果没有返回-1、值类型和引用类型在内存上存储的地方不一样。
    8)、LastIndexOf():判断某个字符串在字符串中最后一次出现的位置,如果没有同样返回-1
    9)、StartsWith():判断以....开始
    10)、EndsWith():判断以...结束
    11)、Replace():将字符串中某个字符串替换成一个新的字符串
    12)、Contains():判断某个字符串是否包含指定的字符串
    13)、Trim():去掉字符串中前后的空格
    14)、TrimEnd():去掉字符串中结尾的空格
    15)、TrimStart():去掉字符串中前面的空格
    16)、string.IsNullOrEmpty():判断一个字符串是否为空或者为null
    17)、string.Join():将数组按照指定的字符串连接,返回一个字符串。


    5、继承
    我们可能会在一些类中,写一些重复的成员,我们可以将这些重复的成员,
    单独的封装到一个类中,作为这些类的父类。
    Student、Teacher、Driver 子类 派生类
    Person 父类 基类
    子类继承了父类,那么子类从父类那里继承过来了什么?
    首先,子类继承了父类的属性和方法,但是子类并没有继承父类的私有字段。
    问题:子类有没有继承父类的构造函数?
    答:子类并没有继承父类的构造函数,但是。子类会默认的调用父类无参数的构造函数,
    创建父类对象,让子类可以使用父类中的成员。
    所以,如果在父类中重新写了一个有参数的构造函数之后,那个无参数的就被干掉了,
    子类就调用不到了,所以子类会报错。
    解决办法:
    1)、在父类中重新写一个无参数的构造函数。
    2)、在子类中显示的调用父类的构造函数,使用关键字:base()

    6、继承的特性
    1、继承的单根性:一个子类只能有一个父类。
    2、继承的传递性

    7、查看类图

    8、object是所有类的基类。


    9、new关键字
    1)、创建对象
    2)、隐藏从父类那里继承过来的同名成员。
    隐藏的后果就是子类调用不到父类的成员。

    (自己的话)

    把因父类和基类重名的方法而产生的警告屏蔽。

    实例:

    无new

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Class1
    10     {
    11         public void Test()
    12         {
    13             Console.WriteLine("C1:我的名字007!");
    14         }
    15     }
    16 }
    父类Class1
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Class2:Class1
    10     {
    11         public new void Test()
    12         {
    13             Console.WriteLine("C2:我的名字007!");
    14         }
    15     }
    16 }
    基类Class2
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Class1
    10     {
    11         public void Test()
    12         {
    13             Console.WriteLine("C1:我的名字007!");
    14         }
    15     }
    16 }
    Main

    警告信息:“ConsoleApplication1.Class2.Test()”隐藏了继承的成员“ConsoleApplication1.Class1.Test()”。如果是有意隐藏,请使用关键字 new。

    有new

    父类Class1
    基类Class2
    Main

    练习:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string name;
    14             Console.Write("请输入一个你想到的名字:");
    15             name = Console.ReadLine();
    16 
    17             Console.WriteLine("你输入的名字长度为:{0}", name.Length);    //name.Length:获取当前字符串的长度
    18 
    19             Console.ReadKey();
    20         }
    21     }
    22 }
    23 
    24 /*
    25  * 请输入一个你想到的名字:007
    26  * 你输入的名字长度为:3
    27  * 请按任意键继续. . .
    28  */
    1
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                string student1;
                string student2;
    
                Console.Write("请学员1输入喜欢的课程:");
                student1 = Console.ReadLine();
                Console.Write("请学员2输入喜欢的课程:");
                student2 = Console.ReadLine();
    
                student1 = student1.ToUpper();
                student2 = student2.ToUpper();
    
                if(student1 == student2)
                {
                    Console.WriteLine("你们两个喜欢相同的课程:{0}!", student1);
                }
                else
                {
                    Console.WriteLine("我们两个喜欢不同的课程,学员1:{0},学员2:{1}", student1, student2);
                }
               
                Console.ReadKey();
            }
        }
    }
    2

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string s = "abc";
    14             char[] a = s.Reverse().ToArray();
    15 
    16             Console.WriteLine(a);
    17             Console.ReadKey();
    18         }
    19     }
    20 }
    1
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string test = "hello c sharp";
    14             string[] a = test.Split(' ');
    15             a = a.Reverse().ToArray();
    16             test = String.Join(" ", a);
    17 
    18             Console.WriteLine(test);
    19             Console.ReadKey();
    20         }
    21     }
    22 }
    2
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string email = "abc@163.com";
    14             string[] a = new string[2];
    15             a = email.Split('@');
    16 
    17             Console.WriteLine("用户名:{0},域名:{1}", a[0], a[1]);
    18             Console.ReadKey();
    19         }
    20     }
    21 }
    3
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.IO;
     7 
     8 namespace ConsoleApplication1
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {        
    14             /*
    15              * 文本文件中存储了多个文章标题、作者,
    16              * 标题和作者之间用若干空格(数量不定)隔开,
    17              * 每行一个,标题有的长有的短,
    18              * 输出到控制台的时候最多标题长度10,
    19              * 如果超过10,则截取长度8的子串并且最后添加“...”,
    20              * 加一个竖线后输出作者的名字。
    21             */
    22 
    23             string path = @"C:UsersJohnDesktop1.txt";
    24             string[] contexts = File.ReadAllLines(path, Encoding.Default);
    25 
    26             for(int i = 0; i < contexts.Length; ++i)
    27             {
    28                 string[] strNew = contexts[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    29                 Console.WriteLine((strNew[0].Length>10?strNew[0].Substring(0,8):strNew[0])+"|"+strNew[1]);
    30             }
    31 
    32                 Console.ReadKey();
    33         }
    34     }
    35 }
    4
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication2
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             /*第一种方法,不推荐
    14             string lanage;
    15             Console.WriteLine("请输入一句话:");
    16             lanage = Console.ReadLine();
    17 
    18             for(int t = 0; t <= lanage.Length - 1; ++t)
    19             {
    20                 if(lanage[t] == 'e')
    21                 {
    22                     Console.Write(t + " ");
    23                 }
    24             }
    25             Console.ReadKey();
    26              */
    27 
    28             //第二种
    29             string lanage;
    30             Console.WriteLine("请输入一句话:");
    31             lanage = Console.ReadLine();
    32             int pos = lanage.IndexOf('e');
    33             Console.Write(pos + " ");
    34 
    35             if(lanage != "")
    36             {
    37                 while(pos != -1)
    38                 {
    39                     pos = lanage.IndexOf('e', pos + 1);
    40                     if(pos == -1)
    41                     {
    42                         break;
    43                     }
    44                     Console.Write(pos + " ");
    45                 }
    46                 Console.Write("
    ");
    47             }
    48             else
    49             {
    50                 Console.WriteLine("当前用户输入为空,不存在e的位置!");
    51             }
    52 
    53             Console.ReadKey();
    54         }
    55     }
    56 }
    5
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication3
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             /*
    14              * (用户输入一句话,判断这句话中有没有邪恶,
    15              * 如果有邪恶就替换成这种形式然后输出,
    16              * 如:老牛很邪恶,输出后变成老牛很**;
    17             */
    18             string test = "老牛很邪恶";
    19             test = test.Replace("邪恶" , "**");
    20             Console.WriteLine(test);
    21             Console.ReadKey();
    22         }
    23     }
    24 }
    6
  • 相关阅读:
    ExtJS小技巧
    Oracle 表的行数、表占用空间大小,列的非空行数、列占用空间大小 查询
    NPM 私服
    IDEA 不编译java以外的文件
    SQL 引号中的问号在PrepareStatement 中不被看作是占位符
    Chrome 浏览器自动填表呈现淡黄色解决
    批量删除Maven 仓库未下载成功.lastupdate 的文件
    Oracle 11g 监听很慢,由于监听日志文件太大引起的问题(Windows 下)
    Hibernate 自动更新表出错 建表或添加列,提示标识符无效
    Hibernate 自动更新表出错 More than one table found in namespace
  • 原文地址:https://www.cnblogs.com/2016Study/p/5538660.html
Copyright © 2011-2022 走看看