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

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

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.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
成人黄色大片在线免费观看| 国产成人精品日本亚洲| 欧美激情综合亚洲一二区| 欧美久久精品一级黑人c片| 456国产精品| 日韩av在线免费观看| 国产在线视频欧美| 欧美巨乳美女视频| 国产一区二区丝袜高跟鞋图片| 琪琪第一精品导航| 国产性猛交xxxx免费看久久| 最新91在线视频| 成人免费xxxxx在线观看| 国产成人精品视频| 亚洲一区二区三区视频| 久久av红桃一区二区小说| 国产精品成人一区二区三区吃奶| www.亚洲人.com| 91超碰中文字幕久久精品| 91日韩在线播放| 亚洲成人网在线观看| 久久久久久午夜| 97久久久久久| 亚洲自拍高清视频网站| 精品久久久久久久久久国产| 欧美大码xxxx| 91av在线看| 日韩精品视频在线| 中文字幕亚洲激情| 亚洲淫片在线视频| 美女999久久久精品视频| 国产成人福利视频| 久久精品福利视频| 98视频在线噜噜噜国产| 国产精品夜间视频香蕉| 亚洲欧洲午夜一线一品| 精品免费在线观看| 久久91亚洲精品中文字幕| 欧美精品免费在线| 欧美国产日韩二区| 国产精品小说在线| 久久久久中文字幕| 国产一区视频在线| 国产69精品久久久| 国产精品影片在线观看| 91精品久久久久久久久久久久久| 九九热r在线视频精品| 国产精品视频资源| 亚洲美腿欧美激情另类| 97超级碰在线看视频免费在线看| 91老司机在线| 亚洲free性xxxx护士hd| 91超碰caoporn97人人| 欧美日韩福利电影| 国产欧美精品va在线观看| 57pao成人国产永久免费| 成人精品一区二区三区电影黑人| y97精品国产97久久久久久| 精品国偷自产在线视频99| 日韩资源在线观看| 欧美日韩福利视频| 久久久久久91香蕉国产| 亚洲最新中文字幕| 97精品一区二区视频在线观看| www.欧美精品| 91精品国产91| 亚洲视频网站在线观看| 国产精品国模在线| 亚洲一区二区三区xxx视频| 国产成人久久久精品一区| 超薄丝袜一区二区| 国产福利精品av综合导导航| 日本高清不卡在线| 国产99视频精品免视看7| 91网站在线看| 国产精品自在线| 欧洲亚洲在线视频| 91精品久久久久久久久不口人| 国产精品夫妻激情| 亚洲欧美日韩区| 欧美激情奇米色| 91精品国产高清久久久久久| 亚洲激情电影中文字幕| 国产69精品久久久久9| 在线免费看av不卡| 成人性生交大片免费观看嘿嘿视频| 国产69精品久久久久9| 久久久久九九九九| 亚洲一区二区三区在线视频| 国产成人精品视| 欧美精品第一页在线播放| 欧美成人免费视频| 久久国产加勒比精品无码| 欧美成人在线免费视频| 久久乐国产精品| 国产精品久久久久久久久久久久久久| 91精品国产高清久久久久久久久| 亚洲女同精品视频| 在线不卡国产精品| 亚洲精品久久久久中文字幕欢迎你| 国产欧亚日韩视频| 欧美不卡视频一区发布| 51精品国产黑色丝袜高跟鞋| 尤物九九久久国产精品的特点| 国产亚洲精品综合一区91| 国产美女久久精品| 在线性视频日韩欧美| 欧美洲成人男女午夜视频| 欧美理论电影在线播放| 亚洲一区二区三区成人在线视频精品| 久久天天躁日日躁| 在线亚洲欧美视频| 日韩有码片在线观看| 亚洲一级黄色av| 欧美激情网站在线观看| 成人乱色短篇合集| 亚洲国产精品专区久久| 一区二区三区美女xx视频| 97热精品视频官网| 日韩欧美亚洲范冰冰与中字| 欧美精品电影免费在线观看| 性欧美xxxx| xvideos国产精品| 亚洲天堂影视av| 热久久美女精品天天吊色| 国产精品99免视看9| 欧美视频在线观看免费网址| 亚洲男人天堂古典| 日韩精品在线视频观看| 国产91精品在线播放| 欧美日韩精品中文字幕| www.久久久久久.com| 国产va免费精品高清在线| 国产精品视频xxx| 日韩免费在线播放| 国产精品精品久久久久久| 国产精品久久久久久av| 日本久久中文字幕| 久久大大胆人体| 亚洲国产成人在线视频| 国产区精品视频| 亚洲淫片在线视频| 在线观看欧美成人| 欧美性黄网官网| 欧美成人网在线| 成人av在线天堂| 精品久久久精品| 成人精品视频在线| 91色视频在线导航| 日韩中文字幕在线视频播放| 欧美自拍视频在线| 永久免费看mv网站入口亚洲| 久久久天堂国产精品女人| 亚洲第一网站男人都懂| 538国产精品一区二区在线| 欧美大片va欧美在线播放| 欧美老女人xx| 在线精品高清中文字幕| 国产在线观看一区二区三区| 国产剧情久久久久久| 国产精品入口免费视频一| 最近2019年手机中文字幕| 91久久国产婷婷一区二区| 中文字幕一精品亚洲无线一区|