麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ChucK初步(10)

2019-11-11 05:08:40
字體:
來源:轉載
供稿:網友

classes and objects

introductionexisting classesworking with objectswriting a classmembers (data + functions)static (data + functions)inheritanceoverloadation (overloading + overriding)

introduction

Chuck implements(實現) an object system that borrows from both C++ and java conventions. In our case this means:

You can define(定義) custom classes as new types and instantiate objects自定義類ChucK supports polymorphic inheritance(多態繼承) (this is the same model used in Java, and also known as virtual inheritance in C++)All object variables(變量) are references (like Java), but instantiation(實例化) resembles(類似于) C++. We will discuss this in detail below.There is a default class library.All objects inherit(繼承) from the Object class (as in Java)

For the sake(目的) of clarity(清楚) we will define(定義) these terms:

a class is an abstraction of data (members) and behavior(行為) (methods)a class is a type.an object is an instantiation of that classa reference variable refers indirectly to an object - it is not the object itself. All ChucK object variables are reference variables(變量) (like in Java).similarly, reference assignment(引用賦值) duplicates a reference to an object and assigns(分配) the reference to a reference variable. The object itself is not duplicated. All ChucK object assignments are reference assignments.所有對象賦值都是引用賦值

built-in classes

ChucK has a number of classes defined(定義) within the language.

Object : base class to all ChucK objects.Event : ChucK’s basic synchronization(同步) mechanism(機制); may be extended(延伸) to create custom Event functionality(功能) (discussed here).Shred : basic abstraction(抽象) for a non-PReemptive ChucK process(非搶占式進程).UGen : base unit generator class (discussed here).

These are some of the more commonly used classes in ChucK.

working with objects

Let’s begin with some examples. For these examples, let’s assume Foo is a defined class.

// create a Foo object; stored in reference variable barFoo bar;

The above code does two things:

a reference variable bar is declared; its type is Foo.a new instance of Foo is created, and its reference is assigned to bar.

Note that in contrast to(與…相比) Java, this statement both declares a reference variable and instantiates a instance of that class and assigns the reference to the variable. Also note that in contrast to C++, bar is a reference, and does not represent the object itself.

To declare a reference variable that refers to nothing (also called a null reference):

// create a null reference to a Foo objectFoo @ bar;

The above code only declare a reference and initializes it to null. (random note: the above statement may be read as “Foo at bar”)

We can assign a new instance to the reference variable:

// assign new instance of Foo to barnew Foo @=> Foo @ bar;// (this statement is equivalent to 'Foo bar', above)

The new Operator creates an instance of a class, in this case Foo. The @=> operator performs the reference assignment. (see here for more information on @=>)

It is possible to make many references to same object:

// make a FooFoo bar;// reference assign to duhbar @=> Foo @ duh;// (now both bar and duh points to the same object)

ChucK objects are reference counted(引用計數) and garbage collection takes place automatically. (note: this is still being implemented!)引用計數,垃圾回收

As stated above(如上所述), a classes may contain data and behavior, in the form of member variables and member functions, respectively. Members are accessed by using ‘dot notation’ - reference.memberdata and reference.memberfunc(). To invoke(調用) a member function of an object (assuming class Foo has a member function called compute that takes two integers and returns an integer):

// make a Foo Foo bar; // call compute(), store result in boo bar.compute( 1, 2 ) => int boo;

writing a class

If a class has already been defined in the ChucK virtual machine (either in the same file or as a public class in a different file) then it can be instantiated similar to primitive types.

Unless declared public, class definitions are scoped to the shred and will not conflict with identically named classes in other running shreds.除非聲明是公共的,類的定義作用于該進程,并將不會與在其他運行進程中的命名相同的類發生沖突。

Classes encapsulate(封裝) a set of behaviors and data. To define a new object type, the keyWord class is used followed by the name of that class.

// define class Xclass X{ // insert code here}

If a class is defined as public, it is integrated(整合) into the central namespace (instead of the local one), and can be instantiated from other programs that are subsequently compiled. 在隨后編譯的其他程序中,公共類也可以被實例化。

There can be at most one public class per file.(每個文件中至多一個公共類)

// define public class MissPopularpublic class MissPopular{ // ...}// define non-public class Flargclass Flarg{ // ...}// both MissPopular and Flarg can be used in this file// only MissPopular can be used from another file

We define member data and methods to specify the data types and functionality(數據類型和功能) required of the class. Members, or instance data and instance functions are associated with individual instances of a class, whereas(然而) static data and functions are only associated with the class (and shared by the instances). 成員 或者 實例數據和實例函數,關聯各個類實例,而靜態數據和函數是關聯于類的,各實例共享。

members (instance data + functions)

Instance data and methods are associated with an object.

// define class Xclass X{ // declare instance variable 'm_foo' int m_foo; // another instance variable 'm_bar' float m_bar; // yet another, this time an object Event m_event; // function that returns value of m_foo fun int getFoo() { return m_foo; } // function to set the value of m_foo fun void setFoo( int value ) { value => m_foo; } // calculate something fun float calculate( float x, float y ) { // insert code } // print some stuff fun void print() { <<< m_foo, m_bar, m_event >>>; }}// instantiate an XX x;// set the Foox.setFoo( 5 );// print the Foo<<< x.getFoo() >>>;// call printx.print();

class constructors

In the initial release(初始版本), we do not support constructors yet. However, we have a single pre-constructor. The code immediately inside a class definiton(定義) (and not inside any functions) is run every time an instance of that class is created. 類實例化時就自動運行類定義里而不是任何函數里的代碼。

// define class Xclass X{ // we can put any ChucK statements here as pre-constructor // initialize an instance data 109 => int m_foo; // loop over stuff for( 0 => int i; i < 5; i++ ) { // print out message how silly <<< "part of class pre-constructor...", this, i >>>; } // function fun void doit() { // ... }}// when we instantiate X, the pre-constructor is runX x;// print out m_foo<<< x.m_foo >>>;

static (data + functions)

Static data and functions are associated with a class, and are shared by all instances of that class – in fact, static elements can be accessed without an instance, by using the name of the class: Classname.element.

// define class Xclass X{ // static data static int our_data; // static function fun static int doThatThing() { // return the data return our_data; }}// do not need an instance to access our_data2 => X.our_data;// print out<<< X.our_data >>>;// print<<< X.doThatThing() >>>;// create instances of XX x1;X x2;// print out their static data - should be same<<< x1.our_data, x2.our_data >>>;// change use one5 => x1.our_data;// the other should be changed as well<<< x1.our_data, x2.our_data >>>;

靜態數據是同一塊內存

inheritance

Inheritance in object-oriented(面向對象的) code allows the programmer to take an existing class to extend(擴展) or alter(改變) its functionality. In doing so we can create a taxonomy(分類法) of classes that all share a specific(特定的) set of behaviors(行為), while implementing(實施) those behaviors in different, yet well-defined(定義明確的), ways. We indicate(表明) that a new class inherits(繼承) from another class using the extends keyword. The class from which we inherit(繼承) is referred to as the parent class, and the inheriting class is the child class. The Child class receives all of the member data and functions from the parent class, although functions from the parent class may be overridden(重寫) ( below ). Because the children contain the functionality(功能) of the parent class, references to instances(實例) of a child class may be assigned(分配) to a parent class reference type.

For now, access modifiers(存取修改器) (public, protected, private) are included but not fully implemented. Everything is public by default.

// define class Xclass X{ // define member function fun void doThatThing() { <<<"Hallo">>>; } // define another fun void hey() { <<<"Hey!!!">>>; } // data int the_data;}// define child class Yclass Y extends X{ // override doThatThing() fun void doThatThing() { <<<"No! Get away from me!">>>; }}// instantiate a YY y;// call doThatThingy.doThatThing();// call hey() - should use X's hey(), since we didn't overridey.hey();// data is also inherited from X<<< y.the_data >>>;

Inheritance provides us a way of efficiently sharing code between classes which perform similar roles. We can define a particular complex pattern of behavior, while changing the way that certain aspects of the behavior operate.

// parent class defines some basic data and methods class Xfunc{ int x; fun int doSomething( int a, int b ) { return 0; }}// child class, which overrides the doSomething function with an addition operationclass Xadds extends Xfunc{ fun int doSomething ( int a, int b ) { return a + b ; }}// child class, which overrides the doSomething function with a multiply operation class Xmuls extends Xfunc{ fun int doSomething ( int a, int b ) { return a * b; }}// array of references to XfuncXfunc @ operators[2];// instantiate two children and assign reference to the array new Xadds @=> operators[0];new Xmuls @=> operators[1];// loop over the Xfuncfor( 0 => int i; i < operators.cap(); i++ ){ // doSomething, potentially different for each Xfunc <<< operators[i].doSomething( 4, 5 ) >>>;}

because Xmuls and Xadds each redefine(重新定義) doSomething( int a, int b ) with their own code, we say that they have overridden(重寫) the behavior of the parent class. They observe the same interface, but have potentially different implementation. This is known as polymorphism(多態性).

Overloading

Function overloading in classes is similar to that of regular functions. see functions.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91看片王 | 91久久国产露脸精品国产护士 | 色97色 | 成人一区二区三区在线 | 超久久| 国产精品视频 | 欧美成人黄色小视频 | 欧美国产精品一区二区 | 欧美aⅴ视频 | 国产一级一区 | 国产精品嘿咻嘿咻在线播放 | 久久99国产精品视频 | 日韩视频在线观看免费 | xnxx18日本| 国产精品视频在 | 欧美日韩免费一区 | 亚洲天堂在线电影 | 亚洲国产精品久久久久久久久久 | 亚洲视频精品在线 | 久久不射电影 | 成人免费av在线播放 | 性爱视频在线免费 | 午夜视频你懂的 | 国产女厕一区二区三区在线视 | 成人免费一区二区三区在线观看 | 一级做a在线观看 | 国产精品.com | 天天操综| 国产手机国产手机在线 | 国产xxxx岁13xxxxhd | 色无极影院亚洲 | 成年人激情在线 | 日韩精品中文字幕一区二区三区 | 深夜小视频在线观看 | 国产激情视频在线 | 黄色免费在线网站 | 成人在线视频精品 | 热99在线视频 | 4p嗯啊巨肉寝室调教男男视频 | 欧美毛片在线观看 | 韩国19禁在线 |