刘小邦的个人博客分享 http://blog.sciencenet.cn/u/iamliuzhiyong 浮生浪迹笑明月 千愁散尽一剑轻

博文

[机器学习]object-oriented programming

已有 5229 次阅读 2013-9-12 17:06 |个人分类:机器学习|系统分类:海外观察

Inheritance and Polymorphism

Private members are not inherited! Protected members are accessible in subclass. Public members are accessible in subclasses and non-subclasses.

How to pass parameters? super(). The statement super() calls the superclass's constructor. If the class declaration doesn't explicitly designate the superclass with the extends clause, then the class's superclass is object class.

Method overriding means same name, same parameter and signature.

Polymorphism means the correct method is selected at runtime based on its runtime type.

Single variable student is not limited to referring to an object from student class but can also refer to any object from descendent classes. However, you cannot make a variable of class refer to an object from superclass.

To determine the class of an object, we use the instanceof operator.

member accessible to an object are also accessible from other objects of the same class. Only the public members of an object are accessible from another object if the two objects belong to different classes.

An abstract class is a class defined with the modifier abstract, and no instances can be created from an abstract class. It ends with a semi colon instead of a method body. Private methods and static methods can not be declared as abstract. We use Java interface to share common behavior among the instances of unrelated classes. And one class can implement multiple interfaces. We use inheritance, on the other hand, to share common code among the instances of related classes. If an entity A is a specialized form of another entity B, then model them by using inheritance. Declared A as a subclass of B.

Inheritance and polymorphism are powerful language features to develop extensible and modifiable code.

Inheritance mechanism is used to share common code among the related classes.

In heritance is different from java interface, which is used to share common behavior among unrelated classes.

The third visibility modifier is the protected modifier. If no instances are created from a superclass, then define the superclass as an abstract class.

Polymorphic message tell us that the method executed in response to the message will vary according to the class to which the object belongs.

The first statement in a constructor of a subclass must be a call to a constructor of the superclass. If the required statement is not made explicitly, then the statement to call the default constructor of the super class is inserted automatically by the java compiler.

The fact that an object variable can refer to multiple actual types is called polymorphism. Automatically selecting the appropriate method is called dynamic binding. The "is-a" rule states that every object of the subclass is an object of the superclass. Another way of formulating the "is a" rule is the substitution principle. The principle states that you can use a subclass object whenever the program expects a superclass object.

For dynamic binding, the compiler looks at the declared type of object and the method name. The compiler enumerate all methods in the subclass and all public method in the superclass. The compiler determine the types of parameters for best match. This process is called overloading resolution. If the compiler cannot find any method with matching parameter types or if multiple methods all match after applying conversion, the compiler reports an error.  If the method is private, static, final or a constructor, the compiler knows exactly which method to call. This is called static binding. Dynamic binding has a very important property: it makes programs extensible without the need for modifying exisint code.

Other programming languages, in particular c++, allow a class to have more than one superclass. This feature is called multiple inheritance. The designers of Java chose not to support multiple inheritance.

 

 

这个课程的主要目标是对sequential object-oriented programming和相关不同的编程语言convey一个deep understanding。challenge可以被解决通过language feature和programming idioms。特别的,course讨论了language design通过比较solution in languages,比如c++, c#, java, python和scala。

这是一个master program的focus course,而bachelor students会take it 作为elective course。

Lecture1

while program fail; while program language is better than others; introduction and motivation; key idea behind that; history of programming language; imperative; declarative; object oriented; language adoption from imperative? to functional to objected oriention; 1980 graphical user interface; 1980 networks; share data among machines; different language to express; 1990 internet; virtual machine; 2000 muticore;

requirement: three concept core concept language concept

software simulation of the real world; model the entity of the real world; describe the dynamic behavior; reuse the model; documented interface; extendability and adaptability; operating system; label of the button; frame work and functionality; concurrency; one thread to run operating system and another thread to perform functional computation; distributed programming; distribugted programming with concurrency; extendibility and adaptability; model student and profesor; print all the records; print function; reuse and extension; add assistant to system; repository? code duplication;

core requirement: cooperating programming parts with well-defined interface; highly dynamic executation model; classification and specialization; correctness;

the philosophy of object model: make program as far as possible refect that part of reality; object exchange message; identity; object lifecyle; delocate the memory; object has location; behavior; trigger other computation; identity and location; completely different program structure; object module; you only see exteral interface; classification and polymorphism; substitution principle? classification of vertebrates; classification could be a tree or DAGs? polymorphic function and object; core concept; ad-hoc polymorphic? overloading; parametric polymorphic; (cast the type), overloading are espically applied for constructor?professional specialization by adding, refine the behavior; highly dynamic executation model; encapsulation; calsssification; subtype; polymorphism; substitution principle; interfaces and encapsulation;

summary: object model; break the conncet style; express ploymorphic; refine them to much more concrete; core concept and language concept; language connecept; overriding specialize method; language concept allow us to implement core concept in core concept;

course organization: compare the object oriented programming; to wirte for better programming; behavior; object oriented programming; c++ production; java major language; java; classification and specialization;

literature: one slide contains different papers; core java volume 1 fundamentals by Horstmann, Cay S and Cornell. writeen english; admission to the exam; four in-class assesment; certain level, but not contribute to the final grade; quite popular; 11. 10,  25. 10; 22. 11, 06, 12

Lecture 2

recap;interface; reuse; classification; goal behind the language; what is a good object programming languages; easy to use; efficiency; safe security; performance; one that make programmers productive; simplicity; syntax and semantics; constructs; recursive call; java type system is decidable, the second goal is expressiveness; heap; safety; compiler cache; conflict with expressiveness and performance; Are there any questions so far?modularity; in c; sequence of memory location; sequence of memory location; look up plus bound check; safety, productivity, and simplicity; memory management confilit a lot about productivity; static safety often conflict with static safety; Backward compatibility; memory management; message not understand; arbitrary location; subtype; behaviour subtyping?untype language means do not classify values into types; weakly-typed language; the third category is strongly typed languages; the key method here;types are a set of values shoaring some properties; support on the fly type; cast; the dynamic type; when I see a bird behavie like a bird, I call the bird a duck.

student(name regNum print lastName) Person(name print lastName) so student can substitute persion, but the other way wrong. so student is the subtype.   q1: what does StudentC mean here?  when objects access field and methods of other objects, message cannot be understood. type system can be used to detect such errors. q2: what is the meaning of "does not have the accessed field or method"? Applet(Java) get access to system resource through API, API control can be implemented in API, which is also called security manager. code must be prevented from by-passing API, type system; type system is a tractable syntatic method for proving absence of certain program behaviors by classifying phrases according to the kinds of values they compute. syntactic: rules are based on form, not behavior; phrases means expressions, methods, kind of values means types; untyped languages, such assembler; weekly typed languages; such c c++; classify values into types, but do not strctly enforce additional restriction; strongly typed languages can enforce all operations to arguments of appropriate types, such as c#, eiffel; q3: could you give some example here? static type checking; types of variables can be delcared explictly or inferred; types of expression can be derived from the types of the constituents; type rules are checked at compile time to check whether a program is correctly checked; q4: don't understand the example for dynamic type checking. Most static system rely on dynamic checks for certain operation; static check need to approximate run-time behavior; dynamic check support on-the-fly code generation and dynamic class loading; don't understand static type and dynamic type, nominal type and structural type; object of subtypes can be used wherever the objects of supertypes are expected; this is substitution principle; subtype objects can understand at least the message that supertype objects can understand; subtype object can understand at least the messages that the supertype objects can understand, this is syntatic classification; subtype provide at least the behavior of supertype objects; the subtype relation correspond to the subset relation; noiminal type means determine membership based on type names, determined subtype relations based on explict declaration; structural type system determine type membership and type relationship based on avability of method and field; subtype system can understand at least the messages that supertype object can understand;subtype has wider interface than supertype object; subtype objects have wider interface than supertype objects; include the existance methods and fields; acessibility of methods and fields; types of methods and fields; subtype may add but not remove methods and fields; an overiding method must not be less accessible than the methods it override, an overriding function must not require more specific parameter types than the method it override, an overiding function must not have more general result type than the method it override; subtypes must not change the types of field; q4: what does regard field as a pair of getter and setter method mean?properties should also include the behavior of the object;expressed as interface specification; precondition hold the rules before the method body is executed.postcondition have to hold in a state after the method body has terminated; object invariants describe consistency criteria for objects; invariant have to hold in all states, in which an object can be accessed by other objects; invariant have to hold in pre and post states of method executation but may be violated temporarily in between; history constrains descirbe how objects how object evolve overtime; history constraints;relate visible states;subtype objects must have fulfil contracts of supertypes; overiding methods have weaker preconditions than corresponding super type methods; overriding method may have stronger postconditions than corresponding supertype methods; subtype may have stronger invariants; subtype objects must fulfil contracts of supertypes; subtypes can have stronger invariants; stronger history constraints; overriding method can have weaker preconditions and stronger postconditions; the concept is called behavior subtyping; often implemented  via specification inheritance; static checking of behavioral subtypeing;  for each overide, check for all parameters, heaps and results; check entailment for all arguments; checking entailment for all arguments; heaps and results is not possible at run time; the run-time checker need to decide which pre and post condition to check;


Exercise 1

40% people failed. It is really important to do the exericese. language features; jam's presentation;

Lecture 3

force all operation to a certain type; strong typing system; two noimal structure; subtype behave as supertype objects; shareing the same properties; noimal subtype; one has explict delcaration, another with methods and fields; nomial subtype and structual subtype; subtype provide at least all the properties from supertype; accesibility; private and public; overidloading function should have the same accessibility; overriding method should require more specific parameter types than the super mehtod; super class has wider parameter type; make more accessiable not less accessiable; overiding field; subtypes musnot change the field types; field are bound statically; contrivaiant parameter; covariant; incovariant; how should define subtype in array? exprssiviness is better if allowed; run time check for array update; many methods with the same name but different signatures for overloading; noimal; generalize reuse; resident, employee are both subtype of person; inheritance; take some from libray and extend your own; structual subtyping; overhead of static subtyping; dynamic structural subtyping has great flexicibility; property is just syntax; object invariant; postcoditon and precondition has visible state; consisteny property; contract require the line shouldn't be null; stactic contract; checking; invariant?  

structual tpe system: determin type membership and subtype relation based on availability of methods and fields;

考点1: subtype objects have wider interface than supertype objects, which include existence of methods and fields, accessibility of methods and field; types of methods and fields; thus B=C<: A

考点2:  an overriding method  require less specific parameter types than the methods it override;  thus D<: E= G

考点3:  an overriding method require less general result type than the methods it override; thus E=G<:F

Q: what does except the reflexive and transive closure of above mean?

考点4:  in java and c#, arrays are covariant., if S<:T, then S[]<:T[]. Each array update requires a run-time type check. covariant allow one to write methods that work for all arrays; the designer resolved the tradeoff between expressiveness and and static safety in favor of expressiveness;

考点5: an overiding method must not less accessible than the methods it overide. At run time, m could access the a private of method of Sub, thereby violating the information hidding

考点6: statically type-safe object-oriented languages guarantee the following type invariant: in every executation state, the type of the value held by variance v is a subtype of the declared type of v.


问题1: what does covariant result mean? what does contravariant parameter mean?

问题2: what does dynamic type mean? what is the differene with static type system? what does in every executation state, the type of the vaule held by variance v is the subtype of the declared type of v.







http://www.pm.inf.ethz.ch/education/courses/coop




https://blog.sciencenet.cn/blog-942948-724361.html

上一篇:[机器学习]machine learning
下一篇:[机器学习]user interface engineering
收藏 IP: 129.132.130.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 15:26

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部