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

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

ChucK初步(10)

2019-11-11 04:01:15
字體:
來源:轉載
供稿:網友

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
国产aⅴ夜夜欢一区二区三区| 一区二区在线视频播放| 日本成人在线视频网址| 日韩a**中文字幕| 亚洲激情在线观看| 有码中文亚洲精品| 成人中文字幕在线观看| 激情成人中文字幕| 成人有码在线视频| 在线日韩精品视频| 成人xvideos免费视频| 亚洲最大在线视频| 国产精品一二区| 日韩风俗一区 二区| 欧美小视频在线观看| 色久欧美在线视频观看| 欧美影院在线播放| 俺去啦;欧美日韩| 久久久久久久久久久国产| 亚洲成人aaa| 国产精品成人免费电影| 91国内产香蕉| 亚洲日韩欧美视频| 亚洲男女性事视频| 国产精品1区2区在线观看| 久久久噜噜噜久久中文字免| 精品国产乱码久久久久久天美| 欧美夫妻性视频| 色综合视频一区中文字幕| 欧美日本在线视频中文字字幕| www日韩中文字幕在线看| 精品久久久999| 日韩av网站在线| 亚洲第一网中文字幕| 国产精品日韩精品| 欧美色视频日本版| 亚洲精品乱码久久久久久金桔影视| 国内精久久久久久久久久人| 97视频在线观看免费高清完整版在线观看| 色妞色视频一区二区三区四区| 久久精彩免费视频| 亚洲国产精品久久久久秋霞蜜臀| 日韩欧美中文免费| 久久国产一区二区三区| 亚洲欧美国产制服动漫| 91成人在线视频| 91在线观看免费观看| 国产精品精品一区二区三区午夜版| 色噜噜久久综合伊人一本| 欧美韩日一区二区| 91精品国产91久久久久久| zzjj国产精品一区二区| 欧美成人国产va精品日本一级| 亚洲女人被黑人巨大进入| 久久亚洲欧美日韩精品专区| 伊人男人综合视频网| 日韩精品极品在线观看播放免费视频| 国产精品一区二区久久精品| 国产裸体写真av一区二区| 久久视频免费在线播放| 中文字幕9999| 日韩在线观看免费高清| 777午夜精品福利在线观看| 欧洲精品在线视频| 久久99精品视频一区97| 久久精品99久久香蕉国产色戒| 4438全国成人免费| 日韩大片免费观看视频播放| 日韩免费在线观看视频| 欧美一级片在线播放| 91热福利电影| 欧美一区第一页| 日韩视频免费中文字幕| 国产91精品在线播放| 亚洲娇小xxxx欧美娇小| 国产va免费精品高清在线观看| 亚洲一区二区三区成人在线视频精品| 国产精品视频xxx| 国产日韩在线亚洲字幕中文| 欧美成人精品一区二区| 日本精品视频在线| 日本一区二区三区在线播放| 亚洲免费精彩视频| 日韩精品久久久久| 91亚洲精品久久久久久久久久久久| 久久久亚洲网站| 日韩欧美国产成人| 岛国av一区二区| 精品日韩中文字幕| 国产97在线|亚洲| 亚洲国产精品美女| 亚洲色在线视频| 18性欧美xxxⅹ性满足| 日韩av在线影院| 日韩美女写真福利在线观看| 亚洲缚视频在线观看| 久久精品中文字幕电影| 91久久精品在线| 亚洲精品资源在线| 国产精品电影网| 宅男66日本亚洲欧美视频| 欧美激情综合亚洲一二区| 激情成人中文字幕| 亚洲偷熟乱区亚洲香蕉av| 国产在线观看一区二区三区| 国产成人精品视频在线| 欧美老女人bb| 尤物九九久久国产精品的特点| 国产精品久久久久久久久久久新郎| 91日韩在线播放| 国产欧美一区二区三区久久人妖| 日韩av一卡二卡| 91精品久久久久久久久中文字幕| 亚洲精品美女在线观看播放| 亚洲xxxxx性| 国产精品女人网站| 欧美人在线观看| 亚洲aaa激情| 色综合伊人色综合网站| 国产午夜精品视频免费不卡69堂| 国产精品白丝av嫩草影院| 午夜美女久久久久爽久久| 日韩电影免费观看在线观看| 欧美激情久久久久| 亚洲香蕉成视频在线观看| 亚洲男女自偷自拍图片另类| 性金发美女69hd大尺寸| 亚洲伊人久久综合| 亚洲美女激情视频| 国产精品福利无圣光在线一区| 国产精品爽爽爽爽爽爽在线观看| 亚洲欧美日韩中文视频| 欧美多人乱p欧美4p久久| 国产乱人伦真实精品视频| 精品久久久久久亚洲国产300| 在线播放国产一区二区三区| 日韩av电影在线网| 中文字幕亚洲欧美一区二区三区| 宅男66日本亚洲欧美视频| 亚洲欧美日韩国产中文专区| 日韩在线一区二区三区免费视频| 亚洲欧洲激情在线| 中文字幕日韩视频| 在线激情影院一区| 97超视频免费观看| 久久久精品亚洲| 欧美日韩在线影院| 欧美中文字幕在线播放| 久久久久久噜噜噜久久久精品| 91精品国产成人www| 午夜伦理精品一区| 国产成人精品视频在线观看| 日韩毛片中文字幕| 欧美激情亚洲激情| 成人精品久久久| 国产精品久久久久aaaa九色| 欧美日韩亚洲天堂| 中国人与牲禽动交精品| 欧美激情女人20p| 欧美片一区二区三区| 国内成人精品视频| 国产精品福利小视频| 国产一区二区香蕉| 欧美黄色三级网站|