流行编程语言的详细对比(7)--对象继承

来源:互联网 发布:java视频教程谁的好 编辑:程序博客网 时间:2024/06/06 02:08

*对象继承*

Java

public class Animal {     private String name;       private int id;     public Animal(String myName, String myid) {         //初始化属性值    }     public void eat() {  //吃东西方法的具体实现  }     public void sleep() { //睡觉方法的具体实现  } } public class Penguin  extends  Animal{ }public interface A {    public void eat();    public void sleep();}public interface B {    public void show();}public class C implements A,B {}

Js

//推荐寄生组合继承function A() {          this.age=10;      }      A.prototype.name="jack";      function B() {          A.apply(this);//继承A的自身属性          this.sex="remale";      }      B.prototype = Object.create(A.prototype);//很关键,创建空对象继承父类的原型,保证不对父类进行修改      //B.prototype = A.prototype;//千万不能这样写,这样写会改变父类原型上的属性的值。      B.prototype.constructor = B;//这句话对结构没有什么影响,只是为了继承后结构的一致性,不然construtor指向A。      var b = new B();      alert("A的原型上的属性:"+b.name);      alert("A的自身属性:"+b.age);      alert("B的自身属性:"+b.sex);

Python

class Parent:        # 定义父类   parentAttr = 100   def __init__(self):      print "调用父类构造函数"   def parentMethod(self):      print '调用父类方法'   def setAttr(self, attr):      Parent.parentAttr = attr   def getAttr(self):      print "父类属性 :", Parent.parentAttrclass Child(Parent): # 定义子类   def __init__(self):      print "调用子类构造方法"   def childMethod(self):      print '调用子类方法 child method'c = Child()          # 实例化子类c.childMethod()      # 调用子类的方法c.parentMethod()     # 调用父类方法c.setAttr(200)       # 再次调用父类的方法c.getAttr()          # 再次调用父类的方法多重继承class SubClassName (ParentClass1[, ParentClass2, ...]):   'Optional class documentation string'   class_suite

Go

/*(1)struct当匿名字段是一个struct的时候,那么这个struct所拥有的全部字段都被隐式地引入了当前定义的这个struct。让我们来看一个例子,让上面说的这些更具体化*/package mainimport "fmt"type Human struct {name stringage intweight int}type Student struct {Human // 匿名字段,那么默认Student就包含了Human的所有字段speciality string}//(2) 对象方法func (b *Box) SetColor(c Color) {b.color = c}func (b Box) SetColor(c Color) {b.color = c}//两者效果一致//(3)对象interfacepackage mainimport "fmt"type Human struct {name stringage intphone string}type Student struct {Human //匿名字段school stringloan float32}type Employee struct {Human //匿名字段company stringmoney float32}//Human实现Sayhi方法func (h Human) SayHi() {fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)}//Human实现Sing方法func (h Human) Sing(lyrics string) {fmt.Println("La la la la...", lyrics)}//Employee重载Human的SayHi方法func (e Employee) SayHi() {fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,e.company, e.phone) //Yes you can split into 2 lines here.}// Interface Men被Human,Student和Employee实现// 因为这三个类型都实现了这两个方法type Men interface {SayHi()Sing(lyrics string)}func main() {mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}Tom := Employee{Human{"Sam", 36, "444-222-XXX"}, "Things Ltd.", 5000}//定义Men类型的变量ivar i Men//i能存储Studenti = mikefmt.Println("This is Mike, a Student:")i.SayHi()i.Sing("November rain")//i也能存储Employeei = Tomfmt.Println("This is Tom, an Employee:")i.SayHi()i.Sing("Born to be wild")//定义了slice Menfmt.Println("Let's use a slice of Men and see what happens")x := make([]Men, 3)//T这三个都是不同类型的元素,但是他们实现了interface同一个接口x[0], x[1], x[2] = paul, sam, mikefor _, value := range x{value.SayHi()}}/*通过上面的代码,你会发现interface就是一组抽象方法的集合,它必须由其他非interface类型实现,而不能自我实现,通过interface实现了duck-typing:即当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子嵌入interface是io包下面的 io.ReadWriter ,他包含了io包下面的Reader和Writer两个interface。*/// io.ReadWritertype ReadWriter interface {ReaderWriter}

Scala
//Scala重写一个非抽象方法,必须用override修饰符。

class Person {  var name = ""  override def toString = getClass.getName + "[name=" + name + "]"}class Employee extends Person {  var salary = 0.0  override def toString = super.toString + "[salary=" + salary + "]"}object Test extends App {  val fred = new Employee  fred.name = "Fred"  fred.salary = 50000  println(fred)}

PHP

class Child extends Parent {   // 代码部分}
原创粉丝点击