成都金牛区建设局网站,深圳燃气公司,数据库 网站 模板,装修设计软件手机版免费版1 【this】 2 指的是明确的标记本类的结构3 当前正在调用类中方法的对象#xff0c;不是一个固定的4 java中以“{}”为界限。如果现在属性名称和参数名称重名#xff0c;那么默认情况下#xff0c;如果没有加任何的限制#xff0c;指的是最近的“{}”内的变量名… 1 【this】 2 指的是明确的标记本类的结构3 当前正在调用类中方法的对象不是一个固定的4 java中以“{}”为界限。如果现在属性名称和参数名称重名那么默认情况下如果没有加任何的限制指的是最近的“{}”内的变量名称。5 在这种情况下明确找到要访问的变量属于类中的属性的时候需要在变量前加this这样就可以准确的进行属性的标记6 1.★★在以后的程序中只要访问类中的属性前面必须加“this”★★7 范例:8 public class Book {9 private String title;10 private double price;11 12 public Book(String title,double price){13 /*同名的属性指的都是系统默认值*/14 title title;15 price price;16 /*this.属性后面是传入的参数*/17 this.title title;18 this.price price;19 }20 21 public String getInfo(){22 return 书名 this.title ,价格 this.price;23 }24 25 public String getTitle() {26 return title;27 }28 29 public void setTitle(String title) {30 this.title title;31 }32 33 public double getPrice() {34 return price;35 }36 37 public void setPrice(double price) {38 this.price price;39 }40 41 public static void main(String[] args) {42 Book book new Book(java,89.5);43 System.out.println(book.getInfo());44 }45 }46 47 调用方法普通方法构造方法48 构造方法之间进行互相调用形式this.(参数,参数)49 2.★严谨地调用本类方法一定加上this★50 注意1.this 一定要放在构造方法首行也尽量放在普通方法首行51 2.类中构造方法间的弧线调用一定要保留出口52 即this 互调构造方法时一定要至少保留一个构造方法没有使用this调用其他构造方法53 范例:54 public class Emp {55 private int empno;56 private String ename;57 private double sal;58 private String dept;59 60 //构造函数可以快捷生成source61 public Emp(){62 this(0,无名氏,0.0,未定);63 }64 65 public Emp(int empno){66 this(empno, 临时工, 800, 后勤部); 67 }68 69 public Emp(int empno,String ename){70 this(empno, ename, 2000, 技术部);71 }72 /*保留了一个构造方法的出口*/73 public Emp(int empno,String ename,double sal,String dept){74 this.empno empno;75 this.ename ename;76 this.sal sal;77 this.dept dept;78 }79 80 public void print(){81 System.out.println(**************************);82 }83 84 public String getInfo(){85 this.print(); //调用本类的方法86 return 雇员编号 this.empno ,\t姓名 this.ename ,\t工资 this.sal ,\t部门: this.dept;87 }88 89 public static void main(String[] args) {90 Emp emp new Emp();91 Emp emp2 new Emp(1);92 Emp emp3 new Emp(2, 李四);93 Emp emp4 new Emp(3, 张三, 1000.8, 行政部);94 System.out.println(emp.getInfo());95 System.out.println(emp2.getInfo());96 System.out.println(emp3.getInfo());97 System.out.println(emp4.getInfo());98 99 }
100 }
101
102
103 【static】
104 ★★编写类时很少用到★★除非类中只有方法这时可以把该方法定义为静态方法
105 类.静态属性
106 类.静态方法
107 1.属性
108 范例
109 public class Book {
110 private String title;
111 private double price;
112
113 static String pub 清华大学出版社;
114
115 public Book(String title,double price){
116 this.title title;
117 this.price price;
118 }
119
120 public void print(){
121 System.out.println(********************);
122 }
123
124 public String getInfo1(){
125 this.print(); //调用本类方法
126 return 书名 this.title ,价格 this.price ,出版社 this.pub;
127 }
128
129 public String getTitle() {
130 return title;
131 }
132
133 public void setTitle(String title) {
134 this.title title;
135 }
136
137 public double getPrice() {
138 return price;
139 }
140
141 public void setPrice(double price) {
142 this.price price;
143 }
144
145 public static void main(String[] args) {
146 Book book1 new Book(java,89.5);
147 Book book2 new Book(Android,12);
148 Book book3 new Book(Oracle,20);
149 /*静态属性只要一个对象修改了属性的内容所有对象的static属性的内容就都将一起改变*/
150 //book1.pub 北京大学出版社;
151 Book.pub 北京大学出版社;
152 System.out.println(book1.getInfo1());
153 System.out.println(book2.getInfo1());
154 System.out.println(book3.getInfo1());
155 }
156 }
157
158 2.方法
159 范例
160 package march_21;
161
162 public class Book {
163 private String title;
164 private double price;
165
166 static String pub 清华大学出版社;
167
168 public Book(String title,double price){
169 this.title title;
170 this.price price;
171 }
172 /*静态方法*/
173 public static void setPub(String p){
174 pub p;//this不能点静态属性
175 }
176
177 public void print(){
178 System.out.println(********************);
179 }
180
181 public String getInfo1(){
182 this.print(); //调用本类方法
183 return 书名 this.title ,价格 this.price ,出版社 this.pub;
184 }
185
186 public String getTitle() {
187 return title;
188 }
189
190 public void setTitle(String title) {
191 this.title title;
192 }
193
194 public double getPrice() {
195 return price;
196 }
197
198 public void setPrice(double price) {
199 this.price price;
200 }
201
202 public static void main(String[] args) {
203 /*没实例化对象也可以利用类名直接调用*/
204 Book.setPub(北京大学出版社);
205
206 Book book1 new Book(java,89.5);
207 Book book2 new Book(Android,12);
208 Book book3 new Book(Oracle,20);
209 System.out.println(book1.getInfo1());
210 System.out.println(book2.getInfo1());
211 System.out.println(book3.getInfo1());
212 }
213 }
214
215 ★static可以不new直接调用非static一定要new才能调用★
216 1.static方法不能直接访问非static属性或者方法只能调用static属性或方法
217 2.非static方法可以调用static的属性或方法不受限制
218
219 main函数中String[] args:程序运行的时候传递的参数形式TestDemo 参数 参数 参数
220 如果参数本身有空格形式TestDemo Hello World Hello MLDN
221
222 功能一实现类实例化对象个数的统计
223 希望每当实例化一个类对象的时候都可以打印一个信息产生的第x个实例化对象。
224 因为只要是新的实例化对象产生了那么一定会去调用类中的构造方法
225 所以可以在构造方法里面增加一个统计数据的操作每当新对象产生之后统计的内容就自增一个。
226 范例
227 public class BookStatic {
228 private static int num 0;
229
230 public BookStatic(){
231 num ;
232 System.out.println(这是第 num 个产生的对象);
233 }
234
235 public static void main(String[] args) {
236 new BookStatic();new BookStatic();new BookStatic();
237 new BookStatic();new BookStatic();new BookStatic();
238 }
239 }
240
241 功能二实现属性的自动设置
242 例如现在某一个类有一个无参构造方法一个有参构造方法有参构造主要的目的是传递一个title属性
243 但是希望不管调用的是无参的还是有参的构造都可以为title设置内容(尽量不使用重复的内容设置)。
244 范例
245 public class BookStatic {
246 private static int num 0;
247 private String title;
248
249 public BookStatic(){
250 this(NOTITLE -- num);
251 // num;
252 }
253
254 public BookStatic(String title) {
255 this.title title;
256 }
257
258 public String getTitle(){
259 return this.title;
260 }
261
262 public static void main(String[] args) {
263 System.out.println(new BookStatic(java).getTitle());
264 System.out.println(new BookStatic().getTitle());
265 System.out.println(new BookStatic().getTitle());
266 }
267 }
268
269 /*执行先后顺序静态块静态代码静态的构造方法*/
270 public class StaticDemo {
271 public StaticDemo(){
272 System.out.println(静态的构造方法);
273 }
274
275 //数据库用的比较多
276 {
277 System.out.println(这是静态代码);
278 }
279 /*程序运行前加载进去的不管有么有实例化对象*/
280 static{
281 System.out.println(这是静态块);
282 }
283
284 public static void main(String[] args) {
285 StaticDemo staticDemo new StaticDemo();
286 }
287 } 转载于:https://www.cnblogs.com/ivy-xu/p/5304096.html