类与变量
类的定义 | 对象定义 |
---|---|
人 | 小明,小红等 |
我的理解面向对象指实例化类。
package helloword;
public class demo2 {
public static void main(String[] args) {
// int Cat1Age=3;
// String Cat1Name="小白";
// String Cat1Color="白色";
// int Cat2Age=100;
// String Cat2Name="小花";
// String Cat2Color="花色";
//创建猫对象
// Cat cat1=new Cat();
// cat1.age=3;
// cat1.name="小白";
// cat1.color="白色";
// Cat cat2=new Cat();
// cat2.age=3;
// cat2.name="小花";
// cat2.color="花色";
// Person a=new Person();
// a.age=12;
// a.name="小明";
// Person b=new Person();
// b=a;
// System.out.println("b.age="+b.age);
// Person c;
// c=b;
// c.age=10;//指向同一个空间,修改哪一个就会都改变
// System.out.println("a的年龄="+a.age);
// System.out.println("b的年龄="+b.age);
// System.out.println("c的年龄="+c.age);
// Person p1=new Person();
// p1.speak();
// Person p2=new Person();
// p2.jisuan();
// p2.jisuan(100);
// Person p3=new Person();
// p3.add(1, 2);
// p3.panduan(4);
// Cat cat=new Cat();
// int res=cat.add(12, 23);
// System.out.println("结果是"+res);
//Cat cat1=new Cat(1,"小花");
}
}
class Cat{
int age;
String name;
String color;
public int add(int i,int j) {
return i+j;
}
// public Cat(int age, String name) {
// age=age;
// name=name;
// System.out.println(age+name);
//
// }}
//class Person{
// int age;
// String name;
// //行为,可以输出我是一个好人。
// //成员方法speak
// public void speak() {
// System.out.println("我是一个好人");
// }
// public void jisuan() {
// int sum=0;
// for(int i=1;i<=1000;i++) {
// sum=sum+i;
//
// }
// System.out.println(sum);
//
// }
// public void jisuan(int n) {
// int sum=0;
// for (int i=1;i<=n;i++) {
// sum=sum+i;
// }System.out.println(sum);
// }
// public void add(int num1,int num2) {
// int sum=0;
// sum=num1+num2;
// System.out.println(sum);
// }
// public void panduan(int m) {
// if(m%2==0) {
// System.out.println("这是偶数");
// }
// else {
// System.out.println("这是奇数");
// }
// }
// }
class Marin{
int age;
String name;
// public void yidong() {
// for (int i = 0; i < 8; i++) {
// for (int j = 0; j < 10; j++) {
// if (i == 0 || i == 7 || j == 0 || j == 9)
// {
// System.out.print("*");
// }
// else {
// if(i==4&&j==6) {
// System.out.print("@");}
// else {
// System.out.print(" ");
// }
// }
// }
// System.out.println();
// }
// }
}
}
类的引用以及类变量
package helloword;
public class demo3 {
public static void main(String[] args) {
// Dog dog1=new Dog(1,"大黄");
// Person p1=new Person(dog1,3,"小明");
// Person p2=new Person(dog1,2,"小花");
// p1.showInfo();
// p1.dog.showInfo();
// Child ch1=new Child(2,"xiaohuang");
// ch1.join();
// Child ch2=new Child(2,"xiaohuang");
// ch2.join();
// Child ch3=new Child(2,"xiaohuang");
// ch3.join();
// System.out.println("共有小孩="+ch3.total);
//如何访问变量?--类.类变量名/对象名.类变量名
}
}
//class Person{
// int age;
// String name;
// Dog dog;
// public Person( Dog dog,int age,String name) {
可读性不好 age=age;
name=name;
// this.age=age;
// this.name=name;
// this.dog=dog;
// System.out.println(this.age+this.name);
// }
// public void showInfo(){
// System.out.println("小明的年龄是="+age);
// }
//}
//class Dog{
// int age;
// String name;
// public Dog(int age,String name) {
// this.age=age;
// this.name=name;
// }
// public void showInfo(){
// System.out.println("大黄的年龄是="+age);
// }
//}
//class Child{
// int age;
// String name;
// static int total=0;
// public Child(int age,String name){
// this.age=age;
// this.name=name;
// total++;
// }
// public void join() {
//
// System.out.println("有一个小孩加进来了");
// }
//
//}
类方法理解
package helloword;
public class demo4 {
static int i=1;//即使不创建对象变量,就存在代码区,
static {
System.out.println("a");//该静态区域块,只执行一次
i++;
}
public demo4() {
System.out.println("b");
i++;
}
public static void main (String[]args) {
demo4 p1=new demo4();
System.out.println(p1.i);
demo4 p2=new demo4();
System.out.println(p2.i);
}
}
类方法
package helloword;
//类方法
public class demo5 {
public static void main(String[] args) {
Apple apple1=new Apple("红色");
Stu stu1=new Stu(3,"梁鹏伟",13800,apple1);
Stu stu2=new Stu(3,"梁鹏伟",13800,apple1);
Stu stu3=new Stu(3,"梁鹏伟",13800,apple1);
Stu stu4=new Stu(3,"梁鹏伟",13800,apple1);
System.out.println("总共学费数="+stu2.totalfee);
System.out.println("总共学费数="+stu1.add());
stu1.apple.showInfo();
Clerk clerk1= new Clerk(3,"梁鹏伟",10000.45f);
System.out.println("梁鹏伟的工资是="+clerk1.getsal());
}
}
class Stu{
int age;
String name;
int fee;
Apple apple;
static int totalfee=0;
public Stu(int age,String name,int fee, Apple apple) {
this.age=age;
this.name=name;
this.fee=fee;
this.apple=apple;
totalfee=totalfee+fee;
}
//返回总学费,这是一个类方法(静态方法)
//java中规则:类变量原则上用类方法访问
public int add() {
return totalfee;
}
}
class Apple{
String color;
public Apple(String color) {
this.color=color;
}
public void showInfo() {
System.out.println("梁鹏伟的苹果颜色"+color);
}
}
class Clerk{
public String name;//公开修饰符
private int age;
private float salary;//修饰符私有
public Clerk(int age,String name,float sal) {
this.age=age;
this.name=name;
this.salary=sal;
}
public float getsal() {
return this.salary;
}
}