目录
一 面向对象的概述
1.1对象
在Java中,对象(Object)是指类(Class)的一个实例化(Instantiation)。Java是一种面向对象的编程语言,它的核心思想是将现实世界中的事物抽象成类,并通过创建对象来操作和处理这些类。
对象具有以下特点:
-
状态(State):对象可以具有一些称为属性(Attributes)或成员变量(Instance Variables)的状态信息。这些状态信息描述了对象的特征和属性。
-
行为(Behavior):对象可以执行一些操作,称为方法(Methods)。方法定义了对象可以进行的操作和行为。
-
标识(Identity):每个对象都有一个唯一的标识,可以用来区分不同的对象。Java中通过引用(Reference)来访问对象,引用保存了对象的内存地址。
在Java中,创建一个对象需要先定义一个类,类是对象的模板,描述了对象的属性和行为。然后通过使用关键字new来实例化该类,为对象分配内存空间,并调用构造方法来初始化对象。
1.2类
类是同一类事物的统称,如果将现实世界中的一个事物抽象成对象,类就是这类对象的统称。
鸟类:
1.3封装
面向对象程序设计具有以下特点:封装性、继承性和多态性。
封装是面向对象编程的核心思想。将对象的属性和行为封装起来,其载体就是类,类通常对客户隐藏其实现细节,这就是封装的思想。例如,用户使用计算机时,只需要使用手指敲击键盘就可以实现一些功能,无须知道计算机内部是如何工作的。即使知道计算机的工作原理,但在使用计算机时也并不完全依赖于计算机工作原理等细节。
采用封装的思想保证了类内部数据结构的完整性,使用类的用户不能轻易地直接操作类的数据结构,只能执行类允许公开的数据。这样就避免了外部操作对内部数据的影响,提高了程序的可维护性。
1.4继承
在原有的特性上继承并增加了新的特性。
1.5多态
将父类对象应用于子类的特征就是多态。
二 类
2.1.成员变量
在Java中,对象的属性也称为成员变量,成员变量可以是任意类型,整个类中均是成员变量作用范围。
创建一个鸟类有哪些属性呢?
public class bird {
String wing;//翅膀
String claw;//爪子
String beak;//喙
String feather;//羽毛
}
Java中需要用class关键词来定义类,bird就是类的名称。同时在bird类中定义了一个成员变量,成员变量的类型为string类型。
2.2成员方法
在Java中,使用成员方法对应于类对象的行为
定义成员方法的语法格式如下:
权限修饰符 返回值类型 方法名(参数类型 参数名){
........//方法体
return 返回值;
}
public class Demo {
public int sum() {//方法
return 1+1;
}
public static void main(String[] args) {
Demo d=new Demo();
int a=d.sum();
System.out.println(a);
}
}
2.3权限修饰符
Java中的权限修饰符主要包括private,public,protected,这些修饰符控制着对类和类的成员变量以及成员名的访问方法。
1. 如果一个类的成员变量或成员方法被修饰为private,则该成员变量只能在本类中被使用,在子类中是不可见的。并且对其他包的类也是不可见的。
2. 如果将类的成员变量和成员方法的访问权限设置为public,那么除了可以在本类使用这些数据,还可以在子类和其他包的内存使用。
3.如果一个类的访问权限修饰符为private,那么这个类将隐藏其内的所有数据,以免用户直接访问它。
2.4this关键字
变量与参数重名怎么办?
比如:
public class Book {
String name="abc";//变量name
public void showName(String name) {//参数name
System.out.println(name);
}
}
我们来执行以下:
public class book {
String name="abc";
public void showname(String name) {
System.out.println(name);
}
public static void main(String[] args) {
book a=new book();
a.showname("123");
}
}
可以发现它输出的不是成员变量的值abc而是参数的值123.、
因此我们用到this关键字。this代表类本身的对象。
加入后代码如下:
public class Book {
String name="abc";
public void showName(String name) {
System.out.println(this.name);
}
public static void main(String[] args) {
Book book = new Book();
book.showName("123");
}
}
我们在来执行一下:
public class book {
String name="abc";
public void showname(String name) {
System.out.println(this.name);
}
public static void main(String[] args) {
book a=new book();
a.showname("123");
}
}
this调用自身的构造方法:
public class Bird {
String name;
public Bird() {
}
public Bird(String name) {
this.name = name;
}
}
举个例子:lei
public class EggCake {
int aggcount;
public EggCake(int aggcount) {//强调要加鸡蛋,构造的类
this.aggcount=aggcount;
System.out.println("鸡蛋灌饼里有"+aggcount+"个鸡蛋。");
}
public EggCake() {//默认加一个鸡蛋
this(1);//调用有参数的构造方法
}
public static void main(String[] args) {
EggCake egg1=new EggCake();
EggCake egg2=new EggCake(5);
}
}
【训练一】汽车加油
一辆汽车的油箱为30L,油箱里现在剩余6L的汽油,加油站每五秒为这辆车加2L的汽油直到加满。控制台输出加油过程和加油时间。
public class text {
public text(int V,int SH) {
int c=V-SH;
int time=0;
System.out.println("油箱正在加油中(ing):");
while(c!=0) {
SH=SH+2;
c=V-SH;
time+=5;
System.out.println("油箱加油"+(SH-6)+"L"+"用时"+time+"s");
if(c==0) {
System.out.println("油箱已经加满为"+V+"L."+"本次加油"+(SH-6)+"L."+"共耗时"+time+"s");
}
}
}
public static void main(String[] args) {
text L=new text(30, 6);
}
}
【训练二】交换数组元素
现在有一个整形数组int a[]={1,3,5,7},编写一段代码,将这个数组的第一个元素与第三个元素进行交换,第二个元素与第四个元素进行交换,最后输出数组交换后的结果。
public class Demo {
public Demo(int a[]) {
int temp1,temp2;
temp1=a[0];
a[0]=a[2];
a[2]=temp1;
temp2=a[1];
a[1]=a[3];
a[3]=temp2;
for(int x:a) {
System.out.print(x+" ");
}
}
public static void main(String[] args) {
int a[]= {1,3,5,7};
Demo c=new Demo(a);
}
}
三 类的构造方法
什么是构造方法?
构造方法是一个与类同名的方法,对象的创建就是通过构造方法完成的。
构造方法的特点:
1.构造方法没有返回值。
2.构造方法的名称要与本类的名称相同。
public class Bird {
public Bird() {
}//Bird类的构造方法
}
public class bird {
String wing;//翅膀
String claw;//爪子
String beak;//喙
String feather;//羽毛
public bird(){//做类的初始化操作
feather="红色";
}
public void fly() {
return;
}
}
【训练三】买可乐
张三去KFC买可乐,商家默认不加冰块,但是张三可以要求加3个冰块。请利用构造方法实现上述功能。
public class KFC {
int count;
public KFC(int count) {
this.count=count;
System.out.println("张三要求加"+this.count+"块冰");
}
public KFC() {
System.out.println("商家默认不加块冰");
}
public static void main(String[] args) {
KFC a2=new KFC();
KFC a1=new KFC(3);
}
}
四 静态变量和静态方法
由static修饰的变量和方法被称为静态变量和静态方法。
静态代码块:
static修饰代码区域
public class StaticTest {
static {
System.out.println(name + “静态代码块”);
}
}
public class Demo {
static {
System.out.println("这里是静态代码块");
}
{
System.out.println("这里是非静态代码块");
}
public Demo() {
System.out.println("这里是构造方法");
}
public void show() {
System.out.println("这里是成员方法");
}
public static void main(String[] args) {
Demo A=new Demo();
A.show();
}
}
从这里可以看出静态代码块和非静态代码块是在构造方法之前运行的。
public class Demo {
static String name;
static {
System.out.println("这里是静态代码块"+name);
}
{
System.out.println("这里是非静态代码块"+name);
}
public Demo(String name) {
this.name=name;
System.out.println("这里是构造方法"+name);
}
public void show() {
System.out.println("这里是成员方法"+name);
}
public static void main(String[] args) {
Demo A=new Demo("abc");
A.show();
}
}
静态代码块在声明类对象的时候就会执行。
【应用】
public class People {
String name;
int year;
int Price;
public People(String name,int year)//类的构造方法
{
this.name=name;
this.year=year;
}
public void Price(int year)//成员方法
{
if(year>=18)
{
Price=40;
}else if(year<18) {
Price=20;
}
System.out.print(" "+Price);
}
public static void main(String[] args) {
People people1=new People("李明",20);//构造方法创建对象
People people2=new People("钱丽",16);//构造方法创建对象
People people3=new People("周刚",8);//构造方法创建对象
People people4=new People("吴红",31);//构造方法创建对象
System.out.println("姓名 年龄 票价(元)");
System.out.println("-------------------");
System.out.print(people1.name+" "+people1.year);
people1.Price(people1.year);
System.out.println();
System.out.print(people2.name+" "+people2.year);
people1.Price(people2.year);
System.out.println();
System.out.print(people3.name+" "+people3.year);
people1.Price(people3.year);
System.out.println();
System.out.print(people4.name+" "+people4.year);
people1.Price(people4.year);
System.out.println();
}
}