"世间万物皆对象"
类:是同一类事物的统称,类之中定义的是属性和方法;
对象:表示一个独立的个体,每个对象拥有自己独立的属性,依靠属性来区分不同对象。
类和对象的区别在于类是对象的模板,对象是类的实例。类只有通过对象才可以使用。要先有类才能产生对象 ;类不能直接使用,对象是可以直接定义使用的。
面向对象编程的三大特征:封装,继承,多态 ;
第四特征:抽象,也就是对象和类的概念 ;
类的特性
1)成员变量
成员变量是Java中类的属性,
public class Bird { //鸟类
String Wing ;
String claw ;
String beak ;
int year ;
}
2)成员方法
成员变量是Java中类的行为,
public class Bird {
public void fly(String insect) {}
public void eat(String meat) {}
public void sleep() {}
}
3)局部变量
在成员方法内定义一个变量,称为局部变量 ;局部变量使用时必须被赋值或者初始化,否则会出现编译错误 ;
public class Bird {
String name = "bird.";
public void getName()
{
String id = "Hello," ; //局部变量,初始化
System.out.println(id+this.name) ;
}
public static void main(String[] args)
{
Bird b = new Bird() ;
b.getName() ;
}
}
//Hello,Bird.
4)this关键字
当类中的成员变量和成员方法中的参数重名时,使用this关键字 ;在类中this表示类本身的对象 ;
public class Bird {
String name = "bird";
public void getName(String name)
{
System.out.println(this.name) ; //输出bird , 输出的是成员变量 ;
}
public static void main(String[] args)
{
Bird b = new Bird() ;
b.getName("fish") ;
}
}
5)权限修饰符
Java中修饰权限符包括private(私有的),public(公共的),protected(受保护的),这些修饰符控制着对类和类的成员变量及成员方法的访问 ;
类的构造方法
构造方法是一个和类同名的方法,对象的创建就是通过构造方法完成的。每当类实例化一个对象,类都会自动调用构造方法 ;构造方法也就是对象的初始化方法 ;
构造方法没有返回值,普通没有返回值的方法使用public void method() 定义,但构造方法不用void修饰 ;
1)对象初始化
在构造方法中可以为成员变量赋值,这样实例化一个本类的对象时,相应的成员变量也将被初始化 ;类中没有明确定义构造方法时,编译器默认创建不带参数的构造方法 ;
public class Test {
public static void main(String[] Args){
Test test = new Test() ;
}
}
public class Test {
public Test(int c) { //带参
}
public Test() { //如果想用无参数的构造方法,就再加一个无参数的
}
public static void main(String[] Args){
Test test = new Test(1) ;
}
}
2)使用this关键字
this可以调用类的成员变量和成员方法,并且this还可以调用类中的构造方法
public class Star {
int count ;
public Star(int count) {
this.count = count ; //使用this调用本类成员变量
System.out.println("天上有"+count+"颗星星");
}
public Star() {
this(10) ; //使用this调用本类构造方法
}
public static void main(String[] args) {
Star star = new Star() ; //天上有10颗星星
Star star1 = new Star(100) ; //天上有100颗星星
}
}
3)私有构造方法
可以用private修饰,私有构造方法无法在本类外使用,导致本类无法用new实例化 ,这样也可以控制对象的生成 ;
public class Star {
private Star() {} //私有构造方法
static public Star Pickingstars() { //创建静态方法,返回本类实例对象 ;
return new Star() ;
}
public static void main(String[] args) {
Star star = Star.Pickingstars() ; //创建一个对象,不是通过new实例化的
}
}
//也就是说,你想要星星但是你又不能自己变出来一个,这时候你得去天上摘一个( ̄︶ ̄)↗
静态变量,常量,方法
static关键字(静态修饰符)
由static修饰的变量、常量、方法被称为静态变量、静态常量、静态方法 ;用静态修饰的代码的生命周期是整个代码的生命周期;
静态区是公共内存区,静态区中的变量可以被本类共享 ;
public class Star {
static public int time = 0 ;
public void ac() {
time += 100 ;
}
public void wa() {
time -= 50 ;
if(time < 0) time = 0 ;
}
public static void main(String[] args) {
Star AC = new Star() ;
Star WA = new Star() ;
System.out.println(time) ; // 0
AC.ac() ;
AC.ac() ;
System.out.println(time) ; //200
WA.wa() ;
System.out.println(time) ; //150 ,改变的都是time
}
}
同一个类的不同实例对象,共用同一个静态变量,如果一个对象将其改变,另一个对象的静态变量也会改变 ;
public class Star {
static int a ;
int b ;
public Star(int a , int b){
this.a = a ;
this.b = b ;
}
public static void main(String[] args) {
Star star1 = new Star(1 , 1) ;
Star star2 = new Star(1111 , 2222) ;
System.out.println(star1.a+" "+star1.b) ; //1111 1 ,改变了静态变量的值
System.out.println(star2.a+" "+star2.b) ; //1111 2222
}
}
//两个类的a指向的是同一个值;
静态常量和静态方法
用final static修饰一个成员变量,这个变量就成了静态常量 ;
public class cicle {
final static double PI = 3.14159 ;
public static void main(String[] args) {
Area a = new Area(2.0) ;
Perimeter b = new Perimeter(4) ;
}
}
class Area {
double R ;
public Area(double R) {
this.R = R ;
System.out.println(R*R*cicle.PI) ;
}
}
class Perimeter {
int r ;
public Perimeter(int r) {
this.r = r ;
System.out.println(r*2*cicle.PI) ;
}
}
调用类的静态方法,无需创建类的对象。
public class Star {
static public void Pinking() {
System.out.println("摘星星") ;
}
public static void main(String[] args) {
Star.Pinking() ;
}
}
静态代码块
成员方法只有在调用的时候才能运行,构造方法在new时候运行,静态代码块在非静态代码块之前运行 ;
public class Star {
static {
System.out.println("静态代码块") ;
}
{
System.out.println("非静态代码块") ;
}
public Star(){
System.out.println("构造方法") ;
}
public void method() {
System.out.println("成员方法") ;
}
public static void main(String[] args) {
Star star = new Star() ;
star.method();
}
}
/**
输出:
静态代码块
非静态代码块
构造方法
成员方法
**/
无了,so difficult :(-