unity学习之面向对象技术核心之继承
2014-12-03 20:47
267 查看
今天接着讲面向对象技术的三大核心,属性封装写在了上一篇日志中,这次主要讲继承
继承
(1)继承提供了创建新类的一种方法,继承对开发者来说就是代码共享。
(2)通过继承创建的子类是作为另一个类的扩充或修正所定义的一个类。
(3)子类从超类(父类)中继承所有方法和变量。
(4)子类和超类之间是特化与范化的关系。
子类的声明
(1)语法:子类声明:父类{子类体}
class Son : Father//子类继承父类
(2)子类可以使用父类的protected和public可见的变量和方法,就像这些变量和方法是自己定义的一样。
class Father
{
//可以用public或者private
//protected int a = 2;
public int a = 2;
public Father()
{
Console.WriteLine("我是父类的里面的构造方法");
}
}
(3)私有成员和方法其实已经被继承了,但是它们却不可以被访问,因为私有成员和方法只能被声明它们的类中才可访问,所以看上去 像是没有被继承。
(4)C# 中,如果类声明时没有声明父类,那么缺省为Object 类的子类。C#中的所有类都是System.Object类的子类。
(5)C#中,子类只能继承一个父类,但一个基类可以有多个直接派生类。继承是可以传递的
Sealed
sealed 修饰符,此修饰符会阻止其他类从该类继承。
sealed class A
{
int test;
public void Sum(int i,int j)
{
int sum = i + j;
Console.WriteLine("I am A ,my sum ={0}",sum);
}
}
class B : A
{
public void Minus(int i,int j)
{
int minus = i - j;
Console.WriteLine("I am B ,my minus ={0}", minus);
this.Sum(3, 4); //编译器会报错
}
}
方法隐藏
如果子类方法的方法名和基类的方法名相同时,系统将隐藏基类同名方法,自动调用子类的同名方法
在这里的举例根据前面的例子连接着来:Father--Son--Son2
//Father类
class Father
{
//protected int a = 2;
public int a = 2;
public Father()
{
Console.WriteLine("我是父类的里面的构造方法");
}
public Father(int a)
{
Console.WriteLine("我是父类的里面的带参数的构造方法");
}
public void Get()
{
Console.WriteLine("我是父类的里面的Get()");
}
static void Main(string[] args)
{
}
}
//Son类
class Son : Father//子类继承父类
// sealed class Son:Father//子类继承父类
{
public Son()
: base(10)
{//显示调用父类里面的带参数的构造方法
Console.WriteLine("我是子类的里面的构造方法");
}
static void Main(string[] args)
{
Son son = new Son();//默认先调用父类构造方法,然后再次调用子类构造方法
//son.Get();
// Console.WriteLine( son.a);
Console.ReadKey();
}
}
//Son2类
class Son2 : Son
{
static void Main(string[] args)
{
Son son = new Son();
son.Get();
Console.WriteLine(son.a);
Console.ReadKey();
//a=b b=c--->a=c;
}
}
派生类的建立需要注意:
(1).派生类会继承基类除了构造函数和析构函数的所有成员。
(2).派生类调用构造函数时,会先调用基类的构造函数。默认调用没有参数的构造函数。
(3).用base关键字显式调用基类构造函数(如果基类定义了带有参数的构造函数,那么此构造函数必须被执行,且在派生类中实现该构 造函数,此时我们可以使用base关键字)
更多精彩内容请关注:http://www.gopedu.com/
继承
(1)继承提供了创建新类的一种方法,继承对开发者来说就是代码共享。
(2)通过继承创建的子类是作为另一个类的扩充或修正所定义的一个类。
(3)子类从超类(父类)中继承所有方法和变量。
(4)子类和超类之间是特化与范化的关系。
子类的声明
(1)语法:子类声明:父类{子类体}
class Son : Father//子类继承父类
(2)子类可以使用父类的protected和public可见的变量和方法,就像这些变量和方法是自己定义的一样。
class Father
{
//可以用public或者private
//protected int a = 2;
public int a = 2;
public Father()
{
Console.WriteLine("我是父类的里面的构造方法");
}
}
(3)私有成员和方法其实已经被继承了,但是它们却不可以被访问,因为私有成员和方法只能被声明它们的类中才可访问,所以看上去 像是没有被继承。
(4)C# 中,如果类声明时没有声明父类,那么缺省为Object 类的子类。C#中的所有类都是System.Object类的子类。
(5)C#中,子类只能继承一个父类,但一个基类可以有多个直接派生类。继承是可以传递的
Sealed
sealed 修饰符,此修饰符会阻止其他类从该类继承。
sealed class A
{
int test;
public void Sum(int i,int j)
{
int sum = i + j;
Console.WriteLine("I am A ,my sum ={0}",sum);
}
}
class B : A
{
public void Minus(int i,int j)
{
int minus = i - j;
Console.WriteLine("I am B ,my minus ={0}", minus);
this.Sum(3, 4); //编译器会报错
}
}
方法隐藏
如果子类方法的方法名和基类的方法名相同时,系统将隐藏基类同名方法,自动调用子类的同名方法
在这里的举例根据前面的例子连接着来:Father--Son--Son2
//Father类
class Father
{
//protected int a = 2;
public int a = 2;
public Father()
{
Console.WriteLine("我是父类的里面的构造方法");
}
public Father(int a)
{
Console.WriteLine("我是父类的里面的带参数的构造方法");
}
public void Get()
{
Console.WriteLine("我是父类的里面的Get()");
}
static void Main(string[] args)
{
}
}
//Son类
class Son : Father//子类继承父类
// sealed class Son:Father//子类继承父类
{
public Son()
: base(10)
{//显示调用父类里面的带参数的构造方法
Console.WriteLine("我是子类的里面的构造方法");
}
static void Main(string[] args)
{
Son son = new Son();//默认先调用父类构造方法,然后再次调用子类构造方法
//son.Get();
// Console.WriteLine( son.a);
Console.ReadKey();
}
}
//Son2类
class Son2 : Son
{
static void Main(string[] args)
{
Son son = new Son();
son.Get();
Console.WriteLine(son.a);
Console.ReadKey();
//a=b b=c--->a=c;
}
}
派生类的建立需要注意:
(1).派生类会继承基类除了构造函数和析构函数的所有成员。
(2).派生类调用构造函数时,会先调用基类的构造函数。默认调用没有参数的构造函数。
(3).用base关键字显式调用基类构造函数(如果基类定义了带有参数的构造函数,那么此构造函数必须被执行,且在派生类中实现该构 造函数,此时我们可以使用base关键字)
更多精彩内容请关注:http://www.gopedu.com/
相关文章推荐
- unity学习之面向对象技术核心之封装
- Java 学习 第三章 Java核心-继承与多态
- unity学习之继承和封装
- java核心技术学习笔记7---继承与多态
- java核心学习(8)多继承
- java学习核心(2) java 三大特性继承
- Unity 3D学习---脚本属性封装、继承、方法隐藏
- Java核心技术(第8版)学习笔记_继承(2)_反射
- 【Unity】NGUI插件核心脚本类的继承关系
- Unity实例.003官方示例Survival Shooter Tutorial核心代码学习
- 【Unity】NGUI插件核心脚本类的继承关系
- HTML5学习笔记(十六):原型、类和继承【JS核心知识点】
- [学习笔记] Java核心技术 卷一:基础知识 对象与类、继承(二)
- Unity学习之基础脚本代码抽象类和继承总结
- java核心技术学习笔记-第五章-继承
- java核心卷之继承学习笔记,你真的懂继承了吗
- Unity学习之继承和多态
- Java核心技术学习笔记-继承
- 《项目管理利器Maven》学习(十):Maven核心之聚合和继承
- Hibernate 3.2 学习笔记 映射继承关系