Classes - Java™ Tutorials

文章推薦指數: 80 %
投票人數:10人

The Java Tutorials have been written for JDK 8. ... Here is sample code for a possible implementation of a Bicycle class, to give you an overview of a class ... Documentation TheJava™Tutorials HideTOC ClassesandObjects Classes DeclaringClasses DeclaringMemberVariables DefiningMethods ProvidingConstructorsforYourClasses PassingInformationtoaMethodoraConstructor Objects CreatingObjects UsingObjects MoreonClasses ReturningaValuefromaMethod UsingthethisKeyword ControllingAccesstoMembersofaClass UnderstandingClassMembers InitializingFields SummaryofCreatingandUsingClassesandObjects QuestionsandExercises QuestionsandExercises NestedClasses InnerClassExample LocalClasses AnonymousClasses LambdaExpressions MethodReferences WhentoUseNestedClasses,LocalClasses,AnonymousClasses,andLambdaExpressions QuestionsandExercises EnumTypes QuestionsandExercises Trail:LearningtheJavaLanguage Lesson:ClassesandObjects HomePage > LearningtheJavaLanguage > ClassesandObjects « Previous • Trail • Next » TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedon'ttakeadvantageofimprovementsintroducedinlaterreleasesandmightusetechnologynolongeravailable.SeeJavaLanguageChangesforasummaryofupdatedlanguagefeaturesinJavaSE9andsubsequentreleases.SeeJDKReleaseNotesforinformationaboutnewfeatures,enhancements,andremovedordeprecatedoptionsforallJDKreleases. Classes Theintroductiontoobject-orientedconceptsinthelessontitled Object-orientedProgrammingConceptsusedabicycleclassasanexample,withracingbikes,mountainbikes,andtandembikesassubclasses.HereissamplecodeforapossibleimplementationofaBicycleclass,togiveyouanoverviewofaclassdeclaration.Subsequentsectionsofthis lesson willbackupandexplainclassdeclarationsstepbystep.Forthemoment,don'tconcernyourselfwiththedetails. publicclassBicycle{ //theBicycleclasshas //threefields publicintcadence; publicintgear; publicintspeed; //theBicycleclasshas //oneconstructor publicBicycle(intstartCadence,intstartSpeed,intstartGear){ gear=startGear; cadence=startCadence; speed=startSpeed; } //theBicycleclasshas //fourmethods publicvoidsetCadence(intnewValue){ cadence=newValue; } publicvoidsetGear(intnewValue){ gear=newValue; } publicvoidapplyBrake(intdecrement){ speed-=decrement; } publicvoidspeedUp(intincrement){ speed+=increment; } } AclassdeclarationforaMountainBikeclassthatisasubclassofBicyclemightlooklikethis: publicclassMountainBikeextendsBicycle{ //theMountainBikesubclasshas //onefield publicintseatHeight; //theMountainBikesubclasshas //oneconstructor publicMountainBike(intstartHeight,intstartCadence, intstartSpeed,intstartGear){ super(startCadence,startSpeed,startGear); seatHeight=startHeight; } //theMountainBikesubclasshas //onemethod publicvoidsetHeight(intnewValue){ seatHeight=newValue; } } MountainBikeinheritsallthefieldsandmethodsofBicycleandaddsthefieldseatHeightandamethodtosetit(mountainbikeshaveseatsthatcanbemovedupanddownastheterraindemands). «Previous • Trail • Next» AboutOracle| ContactUs| LegalNotices| TermsofUse| YourPrivacyRights Copyright©1995,2022Oracleand/oritsaffiliates.Allrightsreserved. Previouspage:ClassesandObjects Nextpage:DeclaringClasses



請為這篇文章評分?