对象的使用
我们创建对象一般都是
类名 对象名 = new 类名();
我们要是访问类中的成员时。
对象名.成员变量;
对象名。成员方法();
诶~~~说了。。我还是举例子吧
1 package com.se.hws.obj; 2 3 public class Student { 4 5 String name; 6 int age; 7 8 public void callName(String name){ 9 System.out.println("给"+name+"打电话"); 10 } 11 12 public void say(){ 13 System.out.println("我在说话呢"); 14 } 15 }
1 public class Test { 2 public static void main(String[] args) { 3 Student student = new Student(); 4 5 System.out.println("名字"+student.name);//null 6 System.out.println("年龄"+student.age);//0 7 8 student.age=11; 9 student.name="hws"; 10 11 System.out.println("名字"+student.name);//null 12 System.out.println("年龄"+student.age);//0 13 14 student.callName("hws"); 15 student.say(); 16 } 17 }
浙公网安备 33010602011771号