java听课记录(java听课记录).doc
java听课记录(java听课记录) java听课记录(java听课记录)object类中定义有public stringtostring()方法,返回值是string类型,描述当前对象的有关信息.在进行string与其他类型数据的连接操作时(如system.out.println("info"+person),将自动调用该对象类tostring()方法注:一个字符串和另外一种类型连接时,另外一种类型直接转成string类型,适用于与基本类型的连接.可以根据需要在用户自定义类型中重写tostring()方法object类中定义有:public booleanequals(object obj)方法提供定义对象是否"等于"的逻辑object的equals(加s是第三人称的意思)方法定义为:x.equals(y),相当于x和y是同一个对象的引用时返回true否则返回false j2sdk提供了一些类,如string,date等,重写了object的equals方法,调用这些类的equals方法,x.equals(y),当x和y所引用的对象是同一类对象且属性内容相等时(不一定是相同的对象),返回true否则返回false可以根据需要在用户定义类型中重写equals方法.对象转型一个父类(也称基类)的引用类型变量可以"指向"其子类的对象.一个父类的引用不可以访问其子类对象新增的成员(属性和方法)可以使用引用变量instanceof类名来判断该引用型变量所"指向"的对象是否属于该嘞活该类的子类子类的对象可以当作父类的对象来使用称之为向上转型(u盘casting),反之称之为向下转型(downcasting)注释:父类引用指向子类时,子类自身的属性和方法不可访问如果一定要访问子类属性要进行强制转换思考一下对象转型对可扩展性带来的好处面向对象的核心内容动态绑定和多态动态绑定是指在执行期间(而非编译期)判断所引用对象的实际类型,分局其实际的类型调用其相应的方法将可扩展性达到最好,调用的方法是新new出来的重中之重,要灵活绑定多态存在的必要条件要有继承,要有重写,父类引用指向子类对象用abstract关键字来定义的类叫做抽象类,用bstract定义的方法叫做抽象方法含有抽象方法的类必须被声明为朝向类,抽象类必须被继承,抽象方法必须被重写抽象类不能被实例化抽象方法只需声明不需实现(因为会被子类继承然后子类重写方法)final关键字final的变量的值不能被改变final的成员变量final的局部变量(形参)final的方法不能被重写final的类不能被继承接口多个无关的类可以实现同一个接口一个类可以实现多个无关的接口与继承关系类似,接口与实现类之间存在多态性定义java类的语法格式:-modifier>classextends<>implements interface,interface*)接口的定义接口(interface)是抽象方法和常量值得定义的集合本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,没有变量和方法的实现接口的特点解救可以多重实现接口中声明的属性默认为public staticfinal的,只能是public staticfinal解救中只能定义抽象方法,而且这些方法默认为public的Interfaces caninherit otherinterfaces andadd propertiesand methodsConstruction method:1.The nameof the construction methodmust bethe same as the class name,and thecase must be exactly the same2,the construction method can be modifiedusing fourpermissions modifiers3.The construction method does not havea returnvalue type4,the construction method ofa classcan havemore thanone,mainly byparameter difference5.The methodbody of the construction method canwrite anystatement thatconforms tothe Javasyntax,but mostof the construction methodsare usedto initializedata membersIn Java,the construction method,there is a problemwe need to payspecial attentionto,is thedefault constructor,if atype declarationdoes notshow theconstruction method,then therewill bea defaultmethod,this methodhas noparameters,the methodbody alsois empty,Such aspublic,Customer()However,as longas the class showsthe declaredconstruction method,theconstructionmethod of the lunchdoes notdefault;if youneedtouse it,you mustalso declarethe declarationThe constructionmethod isa specialmethod,different from the general method:1.The nameof theconstructionmethodmustbeexactlythe same astheclassname thatdefines it.There is no return type,not eventhe void2.The invocationof theconstructionmethodis doneusing thenew operationwhen creatingan object,and theconstructionmethodis alsothe object of initialization3.can notbe modifiedby static,final,synchronized,abstract andnative.The constructionmethod cannotinherit fromthe quiltclass Construction methods canbe overloaded.Construction methodwithout parameterscalled thedefault constructor,and thegeneralmethodof constructingmethod cancarry outany activity,but oftenit isdesigned asa varietyof activitiessuch asinitialization,initialize theobject attribute.In Java,must firstset theinitial valueof anyvariable beforeit isused,Java providesa membervariable aninitial valueof tastebud dedicatedto function:constructionmethodconstructionmethod(constructor)isakind ofspecial membermethod,her specialreflected in the followingaspects:1.The constructionmethod nameis thesameastheclassname2,theconstructionmethod doesnot returnany value,and thereisnoreturntype3,each classcan haveanother livemultiple constructionmethods4.Construction methodsare automaticallyexecuted whenobjects arecreated andcan notbe explicitlycalled directlyCommon classString classThe java.lang.String classrepresents immutablecharacter sequences"Xxxxx"is anobjectofthis classThe monmethods toconstruct String:String(String original):creates acopy of the String object forOriginal String(charvalut):creates a Stringobjectwith acharacter numberString(charvalue.int offset.int count):aStringobject iscreated with the countsequence ofcharacters startingwiththeoffset itemwith anarray ofcharacters Stringclass monlyused methods:Public charcharAt(int index)Returns thefirst indexcharacter inthe string Public,int,length()Returns thelength of the string Public,int,indexof(String,STR,int,fromIndex)Returns thefirst positionof theStr inthe string that appearsfromthebeginning ofthe fromIndexPublic,Boolean,equals,IgnoreCase(String,another)Returns thesame stringas another(ignore case)Public,String,replace(char,oldChar,char,newChar)Replace OldCharcharacters withnewChar charactersin a string PublicBoolean startsWith(String prefix(prefix)Determines whetherthe stringstarts withan prefixstring PublicbooleanendsWith(String suffix(suffix)Determines whetherthe stringhas terminatedat thesuffix string Public,String,toUpperCase()Returns astring formingthe uppercaseofthe stringPublic,String,toLowerCase()Returns astring thatis lowercaseinthestringPublicString substring(int beginIndex)Returns thesubstring frombeginIndex tothe endofthestringPublic,String,substring(int,beginIndex,int,endIndex)Returns thesubstring ofthestringstarting frombeginIndex tothe endoftheendIndex Public,String,trim()Returns thestringthatremoves thebeginning andend spacesofthestring Staticoverloading methodPublic,static,String,valueOf(.)Basic typesof datacanbeconverted intostrings:Such as:Public,static,String,valueOf(double,d)Public,static,String,valueOf(int,I)Method publicStringsplit(String regex)can tellastring,separated bythe specifieddelimiter,to returnthe separatedstring array.StringBuffer classJava.lang.StringBuffer standsfor mutablecharacter sequencesStringBuffer issimilar toString,but StringBuffercan changeits stringsCommon constructionmethods forStringBuffer classes:StringBuffer()Creates anempty StringBuffer object thatdoesnotcontain acharacter sequenceStringBuffer(String,STR)Creates aStringBufferobjectthat containsthesamesequence ofcharacters asthe Stringobject strBasic datatype wrapperclass Wrapperclasses suchas Integer,Double,etc.these classesencapsulate acorresponding datatype valueand providea seriesof operationsfor itTake thejava.lang.Integer classas anexample:Constructionmethod:Integer(int,value)Integer(String,s)。 内容仅供参考