




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
SCJP Study Notes: Chapter 1 Declarations and Access ControlChapter 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 Keywordabstractbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodoubleelseextendsfinalfinallyfloatforgotoifimplementsimportinstanceofintinterfacelongnativenewpackageprivateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttryvoidvolatilewhileassertenum1.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( )- 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- Listener 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 publicpublic void modfiyMyValue(int v)/ cant use modifypublic void addXListener(MyListenser m)/ Mismatch2. Declare Classes2.1 Source File Declaration Rules- there can be ONLY ONE public class per src. file- package, import, class- 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 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 dowhat you declareinterface Bounceablevoid bounce( );what compiler seeinterface Bounceablepublic abstract void bounce( );implicitALL interface method mustbe implemented and mark publicclass Tire implements 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 another InterfaceInterface cant implement another interfaceLegal interface declaration:public abstract interface Rollable public interface Rollable Illegal interface method:final void bounce( );static void bounce( );private 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 = default 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 in 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 + Kid = ONLY through inheritancepackage Cert;public class Parent protected int x = 9;package Other;import Cert.Parent;CASE 1class Child extends Parent public void testit( ) CASE 2System.out.println(x); / OKParent p = new Parent( );System.out.println(p.x); / ERRORNeighbor = same package of child instantiate a child objectCASE 3 = cannot access xLocal VariableAccess modifier NOT applied to local var., except finalPublicProtectedDefaultPrivateFrom same classYYYYOther class in same packageYYYNFrom subclass in same packageYYYNFrom subclass outside packageYYNNFrom non-subclass class outside packageYNNNthrough inheritance4.2 Non - Access ModifierFinal Method- final prevent a method from being overridden in subclasscant be modifiedFinal Argumentpublic int getRecord(final int number Abstract Method- if one method declared abstract, the class 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 only 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:within the functionx0, x1void func(int x) void func(char c, 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 will build one for you- constructor can have normal access modifier, except final, abstract- must take same name as the class- cant mark static, final or abstractclass test2 Legal constructortest2 ( ) private test2 (byte b) test2 (int x) Illegal constructorvoid test2 ( ) test ( ) test2 (short s);static test2 ( );4.4 Variable DeclarationPrimitive:Typechar = 16 bit unicodeBoolean = true/falseBitsbyte8short16int32long64float32double64Reference var: e.g. Object O, String s1Instance var: can be marked final, transient, abstract, synchronized, 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 Declaring 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 or protected class Coffee CoffeeSize size; public class CoffeeTest1 public static void main(String args) Coffee drink = new Coffee(); drink.size = CoffeeSize.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 = Coffee2.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 methods Coffee drink = new Coffee(); drink.size = CoffeeSize.BIG; Optional Semicolonenum CoffeeSize BIG, 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 CoffeeSize(HUGE, 1); public CoffeeSize(String enumName, int index) public static void main(String args) System.out.println(CoffeeSize.BIG); Declaring Constructors, Methods and Variables in an enumYou could make some kind of a lookup 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 ounces; 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();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版社区老年人营养配餐服务合同范本
- 2025年二手房买卖合同补充条款及房屋交易合同备案服务协议
- 2025版商铺转租租赁物使用限制与责任界定合同
- 2025版科技项目研发成果托管合作协议
- 2025年度自流平地板买卖合同范本
- 2025版虚拟现实产业发展担保合同
- 2025版牲畜养殖企业承包与养殖产业链合作合同
- 2025年互联网企业知识产权抵押贷款合同
- 2025东莞租赁合同范本(含租赁期限延长)
- 2025版新能源发电设备采购与现场安装维护合同
- 手术室护理相关知识100问课件
- 卫生部《病历书写基本规范》解读(73页)
- 生物必修一课程纲要
- 南方332全站仪简易使用手册
- 人民调解员培训讲稿村级人民调解员培训.doc
- 高低压配电安装工程-技术标部分(共41页)
- 监理规划编制案例
- 文献检索外文数据库
- 图画捉迷藏-A4打印版
- 受限空间作业票
- 盘扣式外脚手架施工方案
评论
0/150
提交评论