亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

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

ChucK初步(10)

2019-11-11 04:30:48
字體:
來源:轉載
供稿:網友

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.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美裸体xxxx极品少妇| 亚洲精品按摩视频| 一区二区三区日韩在线| 久操成人在线视频| 国产深夜精品福利| 视频在线观看一区二区| 国产日韩欧美综合| 亚洲欧美日本另类| 91产国在线观看动作片喷水| 国产精品久久久久久亚洲影视| 久久亚洲成人精品| 亚洲第一区中文99精品| 国产精品第一视频| 美女福利视频一区| 欧美日韩国产999| 成人乱人伦精品视频在线观看| 曰本色欧美视频在线| 性欧美xxxx视频在线观看| 久久99精品久久久久久琪琪| 欧美在线一级va免费观看| 欧美午夜美女看片| 久久精品成人欧美大片古装| 日韩欧美国产黄色| 亚洲第一男人天堂| 亚洲福利视频久久| 日产精品久久久一区二区福利| 国产福利视频一区二区| 国模gogo一区二区大胆私拍| 亚洲黄页网在线观看| 国产欧美精品在线播放| 欧美日韩亚洲一区二区| 一本久久综合亚洲鲁鲁| 欧美精品18videosex性欧美| 国产精品丝袜一区二区三区| 亚洲欧美日韩直播| 日本久久亚洲电影| 日韩中文字幕视频| 在线成人激情黄色| 亚洲第一精品夜夜躁人人躁| 精品国产1区2区| 国产精品视频一| 欧美乱人伦中文字幕在线| 91久久久久久久久久久| 国产亚洲精品久久久久久777| 亚洲电影免费观看高清完整版在线| 国产香蕉精品视频一区二区三区| 2019中文在线观看| 国产精品久久电影观看| 国产精品va在线| 在线播放国产精品| 51视频国产精品一区二区| 神马国产精品影院av| 韩国美女主播一区| 国产在线拍偷自揄拍精品| 亚洲国产精品成人一区二区| 中文日韩在线视频| 欧美性猛交xxxx黑人猛交| 91美女片黄在线观| 国产精品丝袜久久久久久高清| 色妞色视频一区二区三区四区| 国产成一区二区| 久久久久久国产| 色悠悠久久久久| 日韩免费中文字幕| 国产精品99蜜臀久久不卡二区| 奇米影视亚洲狠狠色| 日韩国产激情在线| 亚洲精品电影网在线观看| 精品国产自在精品国产浪潮| 久久久噜噜噜久久中文字免| 亚洲肉体裸体xxxx137| 97成人精品视频在线观看| 日韩av在线天堂网| 国产精品盗摄久久久| 亚州国产精品久久久| 国产日韩欧美在线| 久久久久久久久久久成人| 国产精品∨欧美精品v日韩精品| 亚洲春色另类小说| 国产精品高潮呻吟久久av黑人| 国产成人精品最新| 亚洲精品91美女久久久久久久| 亚洲一区二区国产| 一本色道久久88亚洲综合88| 欧美在线一级va免费观看| 久久久国产视频91| 国产精品一区久久| 亚洲午夜久久久久久久| 国产成人精品久久亚洲高清不卡| 久久久999成人| 亚洲在线www| 久久久亚洲福利精品午夜| 亚洲欧美色婷婷| 亚洲精品永久免费| 91极品女神在线| 91牛牛免费视频| 日韩精品免费一线在线观看| 日本韩国欧美精品大片卡二| 久久精品国产亚洲7777| 在线观看欧美日韩国产| 亚洲国产精品999| 一本一道久久a久久精品逆3p| 国产在线拍揄自揄视频不卡99| 日韩av综合中文字幕| 国产精品va在线| 欧美人交a欧美精品| 大桥未久av一区二区三区| 亚洲偷熟乱区亚洲香蕉av| 日韩欧美精品在线观看| 中文亚洲视频在线| 国产成人高潮免费观看精品| 欧美成人精品不卡视频在线观看| 久久青草福利网站| 亚洲美腿欧美激情另类| 69av成年福利视频| 亚洲女人被黑人巨大进入al| 日韩毛片中文字幕| 国产精品黄视频| 国产福利视频一区| 久久精品视频在线观看| 午夜精品www| 欧美精品videosex极品1| 日韩av电影免费观看高清| 久久久亚洲国产| 午夜精品久久久久久久久久久久久| 欧美视频国产精品| 欧美在线视频免费播放| 国产国产精品人在线视| 日韩中文字幕免费视频| 另类美女黄大片| 成人午夜黄色影院| 久久久在线观看| 57pao国产成人免费| 一区三区二区视频| 国产精品精品一区二区三区午夜版| 欧美一级成年大片在线观看| 欧美亚洲另类制服自拍| 国产男女猛烈无遮挡91| 欧美在线国产精品| 欧美激情一级欧美精品| 97视频在线观看播放| 日韩成人中文电影| 欧美黄色三级网站| 97精品视频在线| 狠狠色狠狠色综合日日小说| 国产精品视频26uuu| 国产精品天天狠天天看| 欧美精品日韩三级| 中文字幕亚洲在线| 一区二区三区无码高清视频| 国产精品免费看久久久香蕉| 91网站免费看| 国产主播精品在线| 亚洲最大在线视频| 97久久精品视频| 国产精品毛片a∨一区二区三区|国| 亚洲一区二区三区四区视频| 日韩欧美中文免费| 日本最新高清不卡中文字幕| 日本欧美在线视频| 国产精品扒开腿做| xvideos成人免费中文版| 亚洲精品之草原avav久久| 久久久久久亚洲精品中文字幕|