Classes - Java™ Tutorials
文章推薦指數: 80 %
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
延伸文章資訊
- 1Classes and Objects in Java - GeeksforGeeks
A class is a user defined blueprint or prototype from which objects are created. It represents th...
- 2Java Class and Objects (With Example) - Programiz
Example: Java Class and Objects ... In the above program, we have created a class named Lamp . It...
- 3Creating a Class in Java - YouTube
- 4Java - Object and Classes - Tutorialspoint
Classes in Java. A class is a blueprint from which individual objects are created. Following is a...
- 5Objects and Classes in Java - Javatpoint
Class in Java