Non-static properties are used when NULL parameter constructs automatically generate objects
Code:
PackageCom.swift;//automatic generation of objects using the parameterless construction method, the serial number is continuously self-increasing Public classPerson {Private intCount//If you are using a non-static property when defining a class, the result is the same, which is 1. Because this count life cycle is short, only inside the object Public intID; PublicString name; Public intAge ; PublicString City; PublicPerson () {Super(); Count++; This. id=count; This. Name= "noname-" +count; This. age=20; This. city= "Shu" +count; } PublicPerson (intID, String name,intage,string City) { This. id=ID; This. name=name; This. age=Age ; This. city=City ; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString getcity () {returnCity ; } Public voidsetcity (String city) { This. City =City ; } PublicString GetInfo () {return"The person was id=" + ID + ", name=" + name + ", age=" + Age + ", city=" +City ; } }
Results:
Use static properties when NULL parameter constructs automatically generate objects
Code:
PackageCom.swift;//automatic generation of objects using the parameterless construction method, the serial number is continuously self-increasing Public classPerson {Private Static intCount//If you are using static properties when defining a class, you will get a different result. Count life cycle long, same as class Public intID; PublicString name; Public intAge ; PublicString City; PublicPerson () {Super(); Count++; This. id=count; This. Name= "NoName" +count; This. age=20; This. city= "Shu"; } PublicPerson (intID, String name,intage,string City) { This. id=ID; This. name=name; This. age=Age ; This. city=City ; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString getcity () {returnCity ; } Public voidsetcity (String city) { This. City =City ; } PublicString GetInfo () {return"The person was id=" + ID + ", name=" + name + ", age=" + Age + ", city=" +City ; } }
Results:
How do you use NULL parameter construction methods in Java to automatically generate objects of different names, what is the difference between using non-static properties and static properties, and why? How to understand the static keyword