收藏 分享(赏)

SCJP_Ch01 Declaration and Access Control.doc

上传人:myw993772 文档编号:6615251 上传时间:2019-04-18 格式:DOC 页数:8 大小:73.50KB
下载 相关 举报
SCJP_Ch01 Declaration and Access Control.doc_第1页
第1页 / 共8页
SCJP_Ch01 Declaration and Access Control.doc_第2页
第2页 / 共8页
SCJP_Ch01 Declaration and Access Control.doc_第3页
第3页 / 共8页
SCJP_Ch01 Declaration and Access Control.doc_第4页
第4页 / 共8页
SCJP_Ch01 Declaration and Access Control.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、SCJP Study Notes: Chapter 1 Declarations and Access ControlPage 1 of 8Chapter 1 Declarations and Access Control1. Identifiers and JavaBean1.1 Legal IdentifiersMust start with a letter, $ or _ , CANT start with numberLegal: _a, $c, _2_w, _$Illegal: :b, -d, e#, .f, 7gJava Keywordabstract boolean break

2、 byte case catchchar class const continue default dodouble else extends final finally floatfor goto if implements import instanceofint interface long native new packageprivate protected public return short staticstrictfp super switch synchronized this throwthrows transient try void volatile whileass

3、ert enum1.2 JavaBeans StandardsJavaBeans = help Java developer create component= class that have properties1. JavaBean Property Naming Rule- if not Boolean, getter method prefix must be get, e.g. getSize( )- if boolean, getStopped( ) or isStopped( )- setter method prefix must be set, e.g. setSize( )

4、- setter method must marked public with void return type, with arg. Represent property value- getter method must marked public with no arg, return type match arg type of setter method2. JavaBean Listener Naming Rule- register listener prefix with add, eg. AddActionListener( )- unreg = remove- Listen

5、er method name must end with “Listener”E.g. valid JavaBean Signature:public void setMyValue(int v)public int getMyValue( )public Boolean isMyStatus( )public void addMyListener(MyListener m)public void removeMyListener(MyListener m)E.g. Invalid JavaBean Signature:void setCustomer(String s) / must be

6、publicpublic void modfiyMyValue(int v) / cant use modifypublic void addXListener(MyListenser m) / MismatchSCJP Study Notes: Chapter 1 Declarations and Access ControlPage 2 of 82. Declare Classes2.1 Source File Declaration Rules- there can be ONLY ONE public class per src. file- package, import, clas

7、s- a file can have more than 1 non-public class2.2 Class Declaration and ModifiersAccess modifier: public, protected, private, default (package access)Non access modifier: strictfp, final, abstractPublic = all package can access, still need to import itFinal = cant be subclassedAbstract class = cant

8、 be instantiatedabstract class Car private double price;public abstract void goFast( ); / end with ; instead of 3. Declare InterfaceInterface = a contract for what a class can do w/o saying how itll dointerface Bounceablevoid bounce( );interface Bounceablepublic abstract void bounce( );class Tire im

9、plements Bounceablepublic void bounce( ) Interface = 100% abstract class= can only have abstract methodAll interface method are implicitly public and abstractAll var. in interface must be public, static and final, i.e. constInterface method must NOT be staticInterface cant extend anything except ano

10、ther InterfaceInterface cant implement another interfaceLegal interface declaration:public abstract interface Rollable public interface Rollable Illegal interface method:final void bounce( );static void bounce( );what you declarewhat compiler seeALL interface method mustbe implemented and mark publi

11、cimplicitSCJP Study Notes: Chapter 1 Declarations and Access ControlPage 3 of 8private void bounce( );protected void bounce( );Declaring Interface ConstantALWAYS: public static final = can be omitted= CANT change the value no matter how you declare4. Declare Class Member4.1 Access Modifierclass = de

12、fault or public (just two)member = public, protected, default, privatePublic Member- access from diff. package, need to import it first3 ways of access a method- in same class- using reference of the class (.operator)- in inherited method (no need .operator)For subclass, if declared public, can be i

13、n diff. packagePrivate Member= subclass cant inherit / overriding itProtected and Default Member (Most confusing and complicated)Almost identical EXCEPT protected member can be accessed by subclass in diff. package= the diff. is ONLY in subclassDefault access = Package restrictionProtected = Package

14、 + Kid = ONLY through inheritancepackage Cert;public class Parent protected int x = 9;package Other;import Cert.Parent;class Child extends Parent public void testit( ) System.out.println(x); / OKParent p = new Parent( );System.out.println(p.x); / ERRORNeighbor = same package of child instantiate a c

15、hild object= cannot access xCASE 1CASE 2CASE 3SCJP Study Notes: Chapter 1 Declarations and Access ControlPage 4 of 8Local VariableAccess modifier NOT applied to local var., except finalPublic Protected Default PrivateFrom same class Y Y Y YOther class in same package Y Y Y NFrom subclass in same pac

16、kage Y Y Y NFrom subclass outside package Y Y N NFrom non-subclass class outside packageY N N N4.2 Non - Access ModifierFinal Method- final prevent a method from being overridden in subclassFinal Argumentpublic int getRecord(final int number Abstract Method- if one method declared abstract, the clas

17、s must be abstract- the first concrete subclass of an abstract class must implement all abstract method- if abstract extends abstract = no need to implement- abstract cant be combined with final or static or privateSynchronized Method- the method can be accessed by only one thread at a time- can onl

18、y be applied to method, NOT var, NOT classpublic synchronized Record getRecord(int id) Method with Variable Argument List (var-args)- must specified type of arg., can be object type- can have other parm, but var-args must be last parm, can only have one var-argLegal:void func(int x) void func(char c

19、, int x) void func(MyObj myobj) Illegal:void func(int x ) void func(int x, char y) void func(String s, byte b) 4.3 Constructor DeclarationConstructor cant have return typeclass test protected test ( ) / constructorprotected void test ( ) / method, NOT constructor- if dont create explicitly, compiler

20、 will build one for youcant be modifiedwithin the functionx0, x1through inheritanceSCJP Study Notes: Chapter 1 Declarations and Access ControlPage 5 of 8- constructor can have normal access modifier, except final, abstract- must take same name as the class- cant mark static, final or abstractclass t

21、est2 test2 ( ) private test2 (byte b) test2 (int x) void test2 ( ) test ( ) test2 (short s);static test2 ( );4.4 Variable DeclarationPrimitive:Type Bitsbyte 8short 16int 32long 64float 32double 64Reference var: e.g. Object O, String s1Instance var: can be marked final, transient, abstract, synchroni

22、zed, strictfp, native, static, public, private, protectedLocal var: must be initialized, ONLY finalfinal var: final referenceTransient var: JVM will ignore the var when serializing the obj.Volatile varStatic varArray = are objectint key; int key ; String test; String mgr ;int 5 test; / ERROR4.5 Decl

23、aring Enumsenum CoffeeSize BIG, HUGE, OVERWHELMING ; CoffeeSize cs = CoffeeSize.BIG;Enums can be declared as their own separate class, or as a class member, however they must not be declared within a method! 1) Declaring an enum outside a class: enum CoffeeSize BIG, HUGE, SUPER / cannot be private o

24、r protected class Coffee CoffeeSize size; Legal constructorIllegal constructorchar = 16 bit unicodeBoolean = true/falseSCJP Study Notes: Chapter 1 Declarations and Access ControlPage 6 of 8public class CoffeeTest1 public static void main(String args) Coffee drink = new Coffee(); drink.size = CoffeeS

25、ize.BIG; / enum outside class* The enum declared like this MUST be public or default2) Declaring an enum inside a class: class Coffee2 enum CoffeeSize BIG, HUGE, OVERWHELMING CoffeeSize size;public class CoffeeTest2 public static void main(String args) Coffee2 drink = new Coffee2(); drink.size = Cof

26、fee2.CoffeeSize.BIG; / enclosing class/ name required ILLEGALpublic class CoffeeTest1 public static void main(String args) enum CoffeeSize BIG, HUGE, OVERWHELMING / WRONG! Cannot declare enums in methodsCoffee drink = new Coffee(); drink.size = CoffeeSize.BIG; Optional Semicolonenum CoffeeSize BIG,

27、HUGE, OVERWHELMING ; o remember enums are not String or into each of the enumerated types (BIG, HUGE) are actually instances of CoffeeSizeo think of an enums as a kind of classclass CoffeeSize public static final CoffeeSize BIG = new CoffeeSize(“BIG“, 0); public static final CoffeeSize HUGE = new Co

28、ffeeSize(“HUGE“, 1); public CoffeeSize(String enumName, int index) public static void main(String args) System.out.println(CoffeeSize.BIG); SCJP Study Notes: Chapter 1 Declarations and Access ControlPage 7 of 8Declaring Constructors, Methods and Variables in an enumYou could make some kind of a look

29、up tableenum CoffeeSize / 8, 10 and 16 are “passed“ as values to the constructorBIG(8), HUGE(10), OVERWHELMING(16); CoffeeSize(int ounces) this.ounces = ounces; / assign the value to an instance variable private int ounces; / an instance variable each enum value has public int getOunces() return oun

30、ces; class Coffee CoffeeSize size; / each instance of Coffee has-a CoffeeSize enumpublic static void main(String args) Coffee drink1 = new Coffee(); drink1.size = CoffeeSize.BIG;Coffee drink2 = new Coffee();drink2.size = CoffeeSize.OVERWHELMING;System.out.println(drink1.size.getOunces(); / prints 8

31、for(CoffeeSize cs: CoffeeSize.values( )System.out.println(cs + “ “ + cs.getOunces( ); / BIG, 8 o Every enum has a static method, values( ), that returns an arrya of the enums values in the order theyre declaredo You can NEVER invoke an enum constructor directly. The enum constructor is invoked autom

32、atically, with the arguments you define after the constant valueDeclare enum that looks like an anonymous inner class:enum CoffeeSize BIG(8), HUGE(10), OVERWHELMING(16) / start a code block that defines the “body“ for this constantpublic String getLidCode() / override the method defined in CoffeeSiz

33、e return “A“; ; / - the semicolon is REQUIRED when you have a bodyCoffeeSize(int ounces) SCJP Study Notes: Chapter 1 Declarations and Access ControlPage 8 of 8this.ounces = ounces; private int ounces;public int getOunces() return ounces; public String getLidCode() / this method is overridden by the OVERWHELMING constantreturn “B“;/ the default value we want to return for CoffeeSize constants

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报