JAVA学习笔记-类变量与类

    xiaoxiao2026-06-18  4

    package com.sec01; public class Demo3_2 { /*static int i=1; static { //该静态区域块,只执行一次 i++; } public Demo3_2(){ i++; }*/ public static void main(String[] args) { // Demo3_2 t1=new Demo3_2(); // System.out.println(t1.i); // Demo3_2 t2=new Demo3_2(); // System.out.println(t2.i); //创建一个学生 Stu stu1=new Stu(29,"aa",240); Stu stu2=new Stu(39,"bb",340); System.out.println(stu2.getTotalFee()); } } //学生 class Stu{ int age; String name; int fee; static int totalFee; public Stu(int age,String name,int fee){ this.age=age; this.name=name; totalFee+=fee; } //返回总学费[这是一个类方法(静态方法)] //java中规则:类变量原则上用类方法去访问和操作 public int getTotalFee(){ age++; return totalFee; } }

    转载请注明原文地址: https://ju.6miu.com/read-1310663.html
    最新回复(0)