-------android培训、java培训、期待与您交流! ----------
面向对象:
什么事对象:对象是类中事实存在的个体。
什么是面向对象:面向对象是将功能封装进对象,强调具备了功能的对象。它是一种思想。
举个例子,假如我们去饭店吃饭,到饭店要找服务员点菜,那么服务员就是一个对象,而至于服务员拿着菜单后怎么把我们的菜端上来,我们不用管,这就叫面向对象。服务员拿着菜单后的一系列行为比如去厨房送菜单以及端菜的动作等等就是一个面向过程。
面向对象的三个特征:封装,继承,多态
类与对象之间的关系:类是对生活中某一类事物的描述(一种抽象概念),对象是该类事物实实在在存在的个体。
类的定义:定义类其实在定义类中的成员(成员变量和成员函数)。
成员变量特点:
成员变量定义在类中,在整个类中都可以被访问。
成员变量随着对象的建立而建立,存在于对象所在的堆内存中。
成员变量有默认初始化值。
局部变量特点:
局部变量只定义在局部范围内,如:函数内,语句内等。
局部变量存在于栈内存中。
作用的范围结束,变量空间会自动释放。
局部变量没有默认初始化值。
创建对象的格式:
类名 对象名称 = new 类名(); 例如:class Person = new Person();
创建对象其实就是在堆内存里开辟存储空间;
创建对象和使用对象范例:
class CellPhone{
String brand = "google";
String color = "red";
double price;
public void show()
{
System.out.println(brand+ "手机是"+color+"色的"+"价值"+price);
}
}
class CellPhoneDemo{
public static void main(String[] args) {
CellPhone cp = new CellPhone();//建立对象
cp.color = "black";//对对象的属性进行修改
cp.show();//使用对象的功能。
}
}
匿名对象特点:
1, 对方法或字段只进行一次调用时;
new Car().show();
new Car().run();
2,可作为实际参数进行传递;
public void show(Car c){ }
new Car().show(new Car());
3,只在堆内存中开辟空间,而没有在栈内存的引用。
特点:1,函数名与类名相同
2,不用定义返回值类型
3,没有具体的返回值。
作用:给对象进行初始化。
注意:
构造方法不能继承,更不可以被重写。
构造方法可以被重载。
this关键字:
特点:this代表本类对象的引用。
什么时候使用this关键字呢?
当在函数内需要用到调用该函数的对象时,就用this。
注意:this不能用在static修饰的方法里和static修饰的代码块里
范例:
class Student
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
}
static关键字:用于修饰成员(成员变量和成员函数)
被修饰后的成员具备以下特点:
1,随着类的加载而加载
2,优先于对象存在
3,被类中所有对象所共享
4,可以直接被类名调用
使用注意:
1,静态方法只能调用静态成员,不能调用非静态成员。非静态方法可以调用静态成员,也可以调用非静态成员。
2,在静态方法中不允许出现this,super关键字。
this代表对象,而静态方法执行时还没有对象呢!
3,主函数是静态的。
静态是有访问局限性的,只能访问静态的内容。
静态成员变量和非静态成员变量的区别?
1,非静态成员变量又称为实例变量。
静态成员变量又称为类变量。
2,非静态成员变量,随着对象的创建而存在,随着对象的消失而消失。
静态成员变量,随着类的加载而存在,随着类的消失而消失。
3,非静态变量存在于对象堆内存中。
静态变量存在于方法区中。
4, 非静态变量只能被对象所调用。
静态变量可以被类名调用,也可以被对象调用。
class B extends A{}
class C extends B{}
3,子类方法声明抛出的异常应比父类方法申明抛出的异常更小或相等。
class Person {
Person() {
System.out.println(("Person"));
}
Person(String name) {
System.out.println("name:" + name);
}
}
public class Chinese extends Person {
Chinese() {
super(); // 调用父类构造函数
System.out.println("chinese");
}
Chinese(String name) {
super(name);// 调用父类具有相同形参的构造函数
System.out.println("name_2:" + name);
}
public static void main(String[] args) {
Chinese ch = new Chinese();
ch = new Chinese("lisi");
}
}
结论:父类的构造函数,既可以给本类对象初始化,也可以给子类对象初始化。
abstract class Student
{
abstract void study();
}
class ProStudent extends Students
{
public void study()
{
System.out.println("Pro Study");
}
}
interface Inter
{
public static final int NUM=3;
public abstract void show();
}
interface InterA
{
public abstract void method();
}
class Text implements Inter,InterA
{
public void show(){}
}
class InterFaceDemo
{
public static void main(String[] args)
{
Text t=new Text;
System.out.println(t.NUM);
System.out.println(t.NUM);
System.out.println(t.NUM);
}
}
class Outer
{
int x=3;
void method()
{
final int y=4;
class Inner
{
void function()
{
System.out.println(y);
}
}
new Inner().function();
}
}
class InnerClassDemo1
{
public static void main(String[] args)
{
new Outer().method();
}
}
abstract class Demo{
abstract void show();
}
class Outer{
private int num = 5;
public void method(){
//匿名内部类: 就是Demo的一个匿名子类对象;
Demo d = new Demo(){
void show()
{
System.out.println("show run..;........."+num);
}
};
d.show();
}
}