JAVA:类,对象,成员属性,成员方法,构造方法,类变量,类方法<2>

来源:互联网 发布:110端口 编辑:程序博客网 时间:2024/06/06 08:45
一.类的定义

一个全面的类定义是比较复杂的,  定义如下:

二.类的对象

1.如何创建对象

类名 对象名=new 类名();

2.如何访问类的成员属性?

对象名.属性名

3.一个简单的程序实例

复制代码
public class Demo1 {    public static void main(String[] args) {        //创建Cat的一个对象        Cat cat1=new Cat();        cat1.age=3;        cat1.name="xiaohua";        //创建Cat的第二个对象        Cat cat2;        //将cat1赋值给cat2        cat2=cat1;        System.out.println(cat2.age+" "+cat2.name);    }}class Cat {    int age;    String name;}
复制代码

运行结果:

3 xiaohua

三.类的成员方法

1.类的成员方法的定义

2.一个简单的程序实例

复制代码
public class Demo2 {    public static void main(String[] args) {        Calculator calculator=new Calculator();        calculator.calculate(100);        System.out.println(calculator.add(100, 100));    }}class Calculator{    //成员方法    public void calculate(int n){        System.out.println(n*(n+1)/2);    }    public int add(int n1,int n2){        return n1+n2;    }}
复制代码

运行结果:

5050
200

四.类的构造方法

1.构造方法的特点

(1).构造方法名和类名相同;

(2).构造方法没有返回值;

(3).主要作用是完成对新对象的初始化;

(4).在创建对象时,系统自动调用该类的构造方法;

(5).一个类可以有多个构造方法;

(6).每个类都有一个默认的构造方法.

2.一个简单的程序实例

复制代码
public class Demo3 {    public static void main(String[] args) {        Person p1=new Person();        System.out.println("姓名:"+p1.name+"年龄:"+p1.age);                Person p2=new Person(10,"xiaoming");        System.out.println("姓名:"+p2.name+"年龄:"+p2.age);    }}class Person {    int age;    String name;    //空构造方法    public Person(){    }    //有参构造方法    public Person(int a,String n){        age=a;        name=n;    }}
复制代码

运行结果:

姓名:null年龄:0
姓名:xiaoming年龄:10

五.类变量与类方法

1.类变量

(1).类变量是与类相关的, 是公共属性;

(2).加上static称为类变量或者静态变量, 否则称为实例变量;

(3).实例变量是属于每个对象个体的属性;

(4).类变量可以通过类名.类变量名或者对象名.类变量名直接访问.

2.类方法

(1).类方法是与类相关的, 是公共的方法;

(2).类方法中不能访问非静态变量;

(3).可以通过类名.类方法名或者对象名.类方法名使用.

3.简单的程序实例

复制代码
public class Demo4 {    public static void main(String[] args) {        Child child1=new Child(8, "xiaohuang");        child1.joinGame();        Child child2=new Child(10, "xiaoming");        child2.joinGame();                //访问类变量        System.out.println("共有"+child1.total+"个小孩参加游戏");        System.out.println("共有"+child2.total+"个小孩参加游戏");        System.out.println("共有"+Child.total+"个小孩参加游戏");    }}class Child {    int age;    String name;    //类变量(静态变量)    static int total=0;    public Child(int age,String name) {        this.age=age;        this.name=name;    }    //统计参加游戏的小孩人数    public void joinGame() {        total++;    }}
复制代码

运行结果:

共有2个小孩参加游戏
共有2个小孩参加游戏
共有2个小孩参加游戏

复制代码
public class Demo5 {    static int i=1;    static {        i++;    }    public Demo5() {        i++;    }    public static void main(String[] args) {        System.out.println(i);                Demo5 d1=new Demo5();        System.out.println(i);            Demo5 d2=new Demo5();        System.out.println(i);    }}
复制代码

运行结果:

2
3
4

复制代码
public class Demo6 {    public static void main(String[] args) {        Stu stu1=new Stu(10,"xiaoming",120);        Stu stu2=new Stu(20,"xiaohuan",320);        System.out.println(Stu.getTotalFee());    }}class Stu {    int age;    String name;    static int totalFee=0;    public Stu(int age,String name,int fee) {        this.age=age;        this.name=name;        totalFee+=fee;    }    //静态变量用静态方法访问    //静态方法只能访问静态变量    //普通方法可以访问普通的成员变量,也可以访问静态变量    public static int getTotalFee() {        return totalFee;    }}
复制代码

运行结果:

440

阅读全文
0 0
原创粉丝点击