C# Classes and Objects - W3Schools
文章推薦指數: 80 %
Everything in C# is associated with classes and objects, along with its attributes and methods. ... prog2.cs. class Car { public string color = "red"; } ... Tutorials References Exercises Videos Menu Login FreeWebsite GetCertified Pro HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOWTO W3.CSS C C++ C# REACT R JQUERY DJANGO TYPESCRIPT NODEJS MYSQL Darkmode Darkcode × Tutorials HTMLandCSS LearnHTML LearnCSS LearnRWD LearnBootstrap LearnW3.CSS LearnColors LearnIcons LearnGraphics LearnSVG LearnCanvas LearnHowTo LearnSass DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery JavaScript LearnJavaScript LearnjQuery LearnReact LearnAngularJS LearnJSON LearnAJAX LearnAppML LearnW3.JS Programming LearnPython LearnJava LearnC LearnC++ LearnC# LearnR LearnKotlin LearnGo LearnDjango LearnTypeScript ServerSide LearnSQL LearnMySQL LearnPHP LearnASP LearnNode.js LearnRaspberryPi LearnGit LearnAWSCloud WebBuilding CreateaWebsiteNEW WhereToStart WebTemplates WebStatistics WebCertificates WebDevelopment CodeEditor TestYourTypingSpeed PlayaCodeGame CyberSecurity Accessibility Blog DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel LearnGoogleSheets XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery × References HTML HTMLTagReference HTMLBrowserSupport HTMLEventReference HTMLColorReference HTMLAttributeReference HTMLCanvasReference HTMLSVGReference GoogleMapsReference CSS CSSReference CSSBrowserSupport CSSSelectorReference Bootstrap3Reference Bootstrap4Reference W3.CSSReference IconReference SassReference JavaScript JavaScriptReference HTMLDOMReference jQueryReference AngularJSReference AppMLReference W3.JSReference Programming PythonReference JavaReference ServerSide SQLReference MySQLReference PHPReference ASPReference XML XMLDOMReference XMLHttpReference XSLTReference XMLSchemaReference CharacterSets HTMLCharacterSets HTMLASCII HTMLANSI HTMLWindows-1252 HTMLISO-8859-1 HTMLSymbols HTMLUTF-8 × ExercisesandQuizzes Exercises HTMLExercises CSSExercises JavaScriptExercises SQLExercises MySQLExercises PHPExercises PythonExercises NumPyExercises PandasExercises SciPyExercises jQueryExercises JavaExercises C++Exercises C#Exercises RExercises KotlinExercises GoExercises BootstrapExercises Bootstrap4Exercises Bootstrap5Exercises GitExercises ExcelExercises Quizzes HTMLQuiz CSSQuiz JavaScriptQuiz SQLQuiz MySQLQuiz PHPQuiz PythonQuiz NumPyQuiz PandasQuiz SciPyQuiz jQueryQuiz JavaQuiz C++Quiz C#Quiz RQuiz KotlinQuiz XMLQuiz BootstrapQuiz Bootstrap4Quiz Bootstrap5Quiz CyberSecurityQuiz AccessibilityQuiz Courses HTMLCourse CSSCourse JavaScriptCourse FrontEndCourse SQLCourse PHPCourse PythonCourse NumPyCourse PandasCourse DataAnalyticsCourse jQueryCourse JavaCourse C++Course C#Course RCourse React.jsCourse Bootstrap3Course Bootstrap4Course XMLCourse CyberSecurityCourse AccessibilityCourse Certificates HTMLCertificate CSSCertificate JavaScriptCertificate FrontEndCertificate SQLCertificate PHPCertificate PythonCertificate DataScienceCertificate Bootstrap3Certificate Bootstrap4Certificate Bootstrap5Certificate jQueryCertificate JavaCertificate C++Certificate C#Certificate React.jsCertificate TypeScriptCertificate XMLCertificate CyberSecurityCertificate AccessibilityCertificate ExcelCertificate × Tutorials References Exercises GetCertified Spaces Videos Shop Pro C#Tutorial C#HOME C#Intro C#GetStarted C#Syntax C#Output C#Comments C#Variables C#DataTypes C#TypeCasting C#UserInput C#Operators C#Math C#Strings C#Booleans C#If...Else C#Switch C#WhileLoop C#ForLoop C#Break/Continue C#Arrays C#Methods C#Methods C#MethodParameters C#MethodOverloading C#Classes C#OOP C#Classes/Objects C#ClassMembers C#Constructors C#AccessModifiers C#Properties C#Inheritance C#Polymorphism C#Abstraction C#Interface C#Enums C#Files C#Exceptions C#HowTo AddTwoNumbers C#Examples C#Examples C#Compiler C#Exercises C#Quiz C#Certificate C#ClassesandObjects ❮Previous Next❯ ClassesandObjects YoulearnedfromthepreviouschapterthatC#isanobject-orientedprogramminglanguage. EverythinginC#isassociatedwithclassesandobjects,alongwithits attributesand methods.Forexample:inreallife,acarisanobject.Thecarhas attributes,suchasweightandcolor,and methods,suchasdriveandbrake. AClassislikeanobjectconstructor,ora"blueprint"forcreatingobjects. CreateaClass Tocreateaclass,usetheclasskeyword: Createaclassnamed"Car"withavariablecolor: classCar { stringcolor="red"; } Whenavariableisdeclareddirectlyinaclass,itisoftenreferredtoasa field(orattribute). Itisnotrequired,butitisagoodpracticetostartwithanuppercasefirstletterwhennamingclasses.Also,itiscommonthatthenameoftheC#fileandtheclassmatches,asitmakesourcodeorganized.Howeveritisnotrequired(likeinJava). CreateanObject Anobjectiscreatedfromaclass.Wehavealreadycreatedtheclassnamed Car, sonowwecanusethistocreateobjects. TocreateanobjectofCar, specifytheclassname,followedbytheobjectname,andusethekeywordnew: Example Createanobjectcalled"myObj"anduse ittoprintthevalueof color: classCar { stringcolor="red"; staticvoidMain(string[]args) { CarmyObj=newCar(); Console.WriteLine(myObj.color); } } TryitYourself» Notethatweusethedotsyntax(.)toaccessvariables/fieldsinsideaclass(myObj.color).Youwilllearnmoreabout fieldsinthenextchapter. MultipleObjects Youcancreatemultipleobjectsofoneclass: Example CreatetwoobjectsofCar: classCar { stringcolor="red"; staticvoidMain(string[]args) { CarmyObj1=newCar(); CarmyObj2=newCar(); Console.WriteLine(myObj1.color); Console.WriteLine(myObj2.color); } } TryitYourself» UsingMultipleClasses Youcanalsocreateanobjectofaclassandaccessitinanotherclass.This isoftenusedforbetterorganizationofclasses(oneclasshasallthe fieldsandmethods,whiletheotherclassholdstheMain()method(codeto beexecuted)). prog2.cs prog.cs prog2.cs classCar { publicstringcolor="red"; } prog.cs classProgram { staticvoidMain(string[]args) { CarmyObj=newCar(); Console.WriteLine(myObj.color); } } TryitYourself» Didyounoticethepublickeyword?Itiscalledanaccessmodifier, whichspecifiesthatthecolorvariable/fieldofCarisaccessibleforotherclassesaswell,suchas Program. Youwilllearnmuchmoreaboutaccessmodifiersandclasses/objectsinthenextchapters. ❮Previous Next❯ NEW WejustlaunchedW3Schoolsvideos Explorenow COLORPICKER Getcertifiedbycompletingacoursetoday! w3schoolsCERTIFIED.2022 Getstarted CODEGAME PlayGame
延伸文章資訊
- 1C# 类(Class) - 菜鸟教程
C# 类(Class) 当你定义一个类时,你定义了一个数据类型的蓝图。 ... <access specifier> class class_name ... public Line(doubl...
- 2C# Class and Objects - TutorialsTeacher
A class can contain one or more constructors, fields, methods, properties, delegates, and events....
- 3DAY 10 類別 - iT 邦幫忙::一起幫忙解決難題,拯救IT 人的一天
任何物件都可以轉成object,因為C# 裡所有型別都衍生於object。 ... 以class 關鍵字來定義一個類別,public 存取修飾詞會在後面說明,Member 也就是我們「類別」的 ...
- 4C# 的class 前面加public 是什麼意思 - J & K
public 說公用類別對於初學者不太容易了解比較白話的意思是公開的資料大家都能存取大家都能看到可以塞…
- 5class 關鍵字- C# 參考
class TestClass { // Methods, properties, fields, events, delegates // and nested classes go here...