Get the App
SLTechnology News&Howtos  ›  Development  › 

Methods for writing simple classes

Shulou Source: shulou.com Published: 2022-06-03 07:27:23 09月22日 Update

This article introduces the relevant knowledge of "the method of writing simple classes". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Simple class writing / / simple class example-Automobile public class MyCar {/ / [1] encapsulates attributes private String colours; public String getColours () {return colours;} public void setColours (String colours) {this.colours = colours } / / [2] write parametric and nonparametric constructors public MyCar (String colours) {this.colours = colours } public MyCar () {} / / [3] overrides the toString () method of the object class-- returns the string description of the object public String toString () {return "MyCar [colours=" + colours + "]" }} calling method public class MyTestMain {public static void main (String [] args) {/ / how to use class MyCar obj=new MyCar ("black") in the main program; System.out.println (obj.toString ()) System.out.println ("Color of output car:" + obj.getColours ()); System.out.println (); System.out.println ("-") / / set car color properties obj.setColours ("red"); System.out.println (obj.toString ()); System.out.println ("output car color:" + obj.getColours ());}} "method of writing simple classes" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Methods colors output content attributes more knowledge practicality learning next main programs examples functions dilemmas characters strings actual objects situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi vpn Shulou Information Huawei