方法的重载、静态变量、静态方法
1、 方法的重载
方法的重载中参数的列表必须不同,即:或者参数个数不同,或者参数类型不同,或者参数的顺序不同
package JAVA语言的特性;
class Cylinder1 {
private int height;
private double radius;
private double pi=3.14;
private String color;
public double setCylinder(double r,int h) {
radius=r;
height=h;
return r+h;
}
public void setCylinder(String str) {
color=str;
}
public void show() {
System.out.println("圆柱体的颜色:"+color);
}
double area() {
return radius*radius*pi;
}
double volume() {
return area()*height;
}
}
public class 例题7_3方法的重载 {
public static void main(String[] args) {
double r_h;
Cylinder1 volu=new Cylinder1();
r_h=volu.setCylinder(2.5,8);
volu.setCylinder("红色");
System.out.println("圆柱的底半径与高之和="+r_h);
System.out.println("圆柱体体积="+volu.volume());
volu.show();
}
}
运行结果:
D:\environment\jdk1.8.0_101\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3\lib\idea_rt.jar=65019:D:\IntelliJ IDEA 2020.3\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\jdk1.8.0_101\jre\lib\charsets.jar;D:\environment\jdk1.8.0_101\jre\lib\deploy.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\dnsns.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\jaccess.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\localedata.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\mysql-connector-java-8.0.15.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\nashorn.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunec.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\zipfs.jar;D:\environment\jdk1.8.0_101\jre\lib\javaws.jar;D:\environment\jdk1.8.0_101\jre\lib\jce.jar;D:\environment\jdk1.8.0_101\jre\lib\jfr.jar;D:\environment\jdk1.8.0_101\jre\lib\jfxswt.jar;D:\environment\jdk1.8.0_101\jre\lib\jsse.jar;D:\environment\jdk1.8.0_101\jre\lib\management-agent.jar;D:\environment\jdk1.8.0_101\jre\lib\plugin.jar;D:\environment\jdk1.8.0_101\jre\lib\resources.jar;D:\environment\jdk1.8.0_101\jre\lib\rt.jar;D:\untitled\out\production\untitled JAVA语言的特性.例题7_3方法的重载
圆柱的底半径与高之和=10.5
圆柱体体积=157.0
圆柱体的颜色:红色
Process finished with exit code 0
2、静态变量
静态变量在定义时用static来修饰,静态变量不需要实例化就可以使用。使用格式有如下两种:
**类名.静态变量名;
** 对象名.静态变量名;
类中若含有静态变量,则静态变量必须独立于方法之外。
package JAVA语言的特性;
class Cylinder7 {
private static int num=0;
private static double pi=3.14;
private int height;
private double radius;
public Cylinder7(double r,int h) {
radius=r;
height=h;
num++;
}
public void count() {
System.out.print("创建了"+num+"个对象:");
}
double area() {
return radius*radius*pi;
}
double volume() {
return area()*height;
}
}
public class 例题7_8静态变量 {
public static void main(String[] args) {
Cylinder7 volu1=new Cylinder7(2.5,5);
volu1.count();
System.out.println("圆柱体1的体积="+volu1.volume());
Cylinder7 volu2=new Cylinder7(1.0,2);
volu2.count();
System.out.println("圆柱体2的体积="+volu2.volume());
}
}
运行结果:
D:\environment\jdk1.8.0_101\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3\lib\idea_rt.jar=55905:D:\IntelliJ IDEA 2020.3\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\jdk1.8.0_101\jre\lib\charsets.jar;D:\environment\jdk1.8.0_101\jre\lib\deploy.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\dnsns.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\jaccess.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\localedata.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\mysql-connector-java-8.0.15.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\nashorn.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunec.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\zipfs.jar;D:\environment\jdk1.8.0_101\jre\lib\javaws.jar;D:\environment\jdk1.8.0_101\jre\lib\jce.jar;D:\environment\jdk1.8.0_101\jre\lib\jfr.jar;D:\environment\jdk1.8.0_101\jre\lib\jfxswt.jar;D:\environment\jdk1.8.0_101\jre\lib\jsse.jar;D:\environment\jdk1.8.0_101\jre\lib\management-agent.jar;D:\environment\jdk1.8.0_101\jre\lib\plugin.jar;D:\environment\jdk1.8.0_101\jre\lib\resources.jar;D:\environment\jdk1.8.0_101\jre\lib\rt.jar;D:\untitled\out\production\untitled JAVA语言的特性.例题7_8静态变量
创建了1个对象:圆柱体1的体积=98.125
创建了2个对象:圆柱体2的体积=6.28
Process finished with exit code 0
3、静态方法
在静态方法中不能使用this或super。
调用静态方法时,可以使用类名直接调用,也可以用某一个具体的对象名来调用,其格式如下:
类名.静态方法名();
对象名.静态方法名();
package JAVA语言的特性;
class Cylinder8 {
private static int num=0;
private static double pi=3.14;
private int height;
private double radius;
public Cylinder8(double r,int h) {
radius=r;
height=h;
num++;
}
//声明count()为静态方法
public static void count() {
System.out.println("创建了"+num+"个对象");
}
double area() {
return radius*radius*pi;
}
double volume() {
return area()*height;
}
}
public class 例题7_9静态方法 {
public static void main(String[] args) {
//在对象产生之前用类名Cylinder调用count()方法
Cylinder8.count();
Cylinder8 volu1=new Cylinder8(2.5,3);
//1。用对象volu1调用方法
volu1.count();
System.out.println("圆柱体1的体积="+volu1.volume());
Cylinder8 volu2=new Cylinder8(1.0,2);
//2.用类名Cylinder8直接调用方法
Cylinder8.count();
System.out.println("圆柱体2的体积="+volu2.volume());
}
}
运行结果:D:\environment\jdk1.8.0_101\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3\lib\idea_rt.jar=65457:D:\IntelliJ IDEA 2020.3\bin" -Dfile.encoding=UTF-8 -classpath D:\environment\jdk1.8.0_101\jre\lib\charsets.jar;D:\environment\jdk1.8.0_101\jre\lib\deploy.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\dnsns.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\jaccess.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\localedata.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\mysql-connector-java-8.0.15.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\nashorn.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunec.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;D:\environment\jdk1.8.0_101\jre\lib\ext\zipfs.jar;D:\environment\jdk1.8.0_101\jre\lib\javaws.jar;D:\environment\jdk1.8.0_101\jre\lib\jce.jar;D:\environment\jdk1.8.0_101\jre\lib\jfr.jar;D:\environment\jdk1.8.0_101\jre\lib\jfxswt.jar;D:\environment\jdk1.8.0_101\jre\lib\jsse.jar;D:\environment\jdk1.8.0_101\jre\lib\management-agent.jar;D:\environment\jdk1.8.0_101\jre\lib\plugin.jar;D:\environment\jdk1.8.0_101\jre\lib\resources.jar;D:\environment\jdk1.8.0_101\jre\lib\rt.jar;D:\untitled\out\production\untitled JAVA语言的特性.例题7_9静态方法
创建了0个对象
创建了1个对象
圆柱体1的体积=58.875
创建了2个对象
圆柱体2的体积=6.28
Process finished with exit code 0
继承、抽象类、接口
1、类的继承
(1)通过extends关键字,可将父类中的非私有成员继承给子类。
(2)在执行子类的构造方法之前,会先调用父类中没有参数的构造方法,其目的是为了帮助继承自父类成员做初始化的操作。
(3)调用父类中某个特定的构造方法要通过super() 语句来调用,且super() 语句必须写在子类构造方法的第一行。super()不能在static环境中使用。
(4)protected 成员方法不仅可以在子类中直接访问,同时也可以在其子类中访问。
(5)覆盖 是指在子类中定义名称、参数个数与类型均与父类中完全相同的方法,用于重写父类中同名方法的使用。
(6)final来修饰成员变量,说明该成员变量是最终变量,即常量;如果用final修饰成员方法,则该成员方法不能再被子类所覆盖,即为最终方法。
(7)equals() 是比较两个字符串中所包含的内容是否相等,"==" 运算符用于比较两个变量本身的值,即两个对象在内存中的首地址。
(8)getClass() 方法的功能是返回运行时对象所属的类;toString() 方法的功能是将调用该方法的对象的内容转换成字符串,并返回其内容。
package 继承;
class Person1 {
private int age;
private String name;
public Person1() {
System.out.println("调用了person类的无参构造方法");
}
public Person1(String name,int age) {
System.out.println("调用了person类的有参构造方法");
this.name=name;
this.age=age;
}
public void show() {
System.out.println("姓名:"+name+" 年龄"+age);
}
}
class Student1 extends Person1{
private String department;
public Student1() {
System.out.println("调用了学生类的无参构造方法");
}
public Student1(String name,int age,String department) {
super(name,age);
this.department=department;
System.out.println("我是"+department+"系的学生");
System.out.println("调用了学生类的有参构造方法");
}
}
public class 例题8_2调用父类中特定的构造方法 {
public static void main(String[] args) {
Student1 stu1=new Student1();//调用无参构造方法
Student1 stu2=new Student1("李四",23,"信息系");
stu1.show();
stu2.show();
}
}
package 继承;
class Person5 {
protected String name;
public Person5(String name) {
this.name=name;
}
}
public class 例题8_8getclass方法的使用 {
public static void main(String[] args) {
Person5 per=new Person5("贾璐璐");
//用对象per来调用getclass()方法
//因为getclass返回值类型是Class型,所以要用一个Class类型的变量obj来接收它
Class obj=per.getClass();
System.out.println("对象per所属的类为:"+obj);
System.out.println("对象per是否为接口:"+obj.isInterface());
}
}
2、抽象类
(1)抽象类的目的是根据格式来创建和修改新的类,不能用new运算符来创建实例对象的类。
(2)abstract不能与private\static\final或native并列修饰同一个方法。
(3)抽象类可以定义构造方法,但需要用protected来修饰,因为它只能被子类的构造方法所调用。
package 抽象类;
import javax.xml.crypto.dom.DOMCryptoContext;
//抽象类不能用 final修饰;abstract不能与private,static,final或native并列修饰同一个方法
abstract class Shape {
protected String name;
//抽象类的一般方法,本方法是构造方法
public Shape(String name) {
this.name = name;
System.out.print("名称" + name);
}
//将求面积的方法声明为抽象方法
abstract public double getArea();
//将求周长的方法声明为抽象方法
abstract public double getLength();
}
class Circle extends Shape{
private final double PI=3.14;
private double radius;
public Circle (String name,double r) {
super(name);
radius=r;
}
//实现抽象类的getArea()方法
public double getArea() {
return radius*radius*PI;
}
@Override
public double getLength() {
return 0;
}
public double Length() {
return 2*radius*PI;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(String name, double width, double height) {
super(name);
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getLength() {
return 2 * (height + width);
}
}
public class 例题8_10 {
public static void main(String[] args) {
//声明父类对象,指向子类对象
Shape circle=new Circle("圆",10.2);
System.out.println(";面积="+ circle.getArea());
System.out.println(";周长="+ circle.getLength());
//声明父类对象,指向子类对象
Shape rect=new Rectangle("长方形",6.5,10.3);
System.out.println(";面积="+rect.getArea());
System.out.println(";周长="+ rect.getLength());
}
}
3、接口
(1)接口中只有常量+抽象方法。
(2)接口中所有的元素都是public修饰的。
(3)接口中抽象方法的public abstract 可以省略.
(4)接口可继承,支持多继承,不能有方法体。
(5)当一个非抽象的类实现接口的话,必须将接口中所有的抽象方法全部实现。
package 接口;
interface Face1 {
static final double PI=3.14;
abstract double area();
}
interface Face2 {
abstract void setColor(String c);
}
interface Face3 {
abstract void volume();
}
public class Cylinder implements Face3 {
protected String color;
private double radius;
private int height;
public Cylinder(double radius, int height) {
this.radius = radius;
this.height = height;
}
//实现Face1接口
public double area() {
return PI*radius*radius;
}
//实现Face2接口
public void setColor(String color) {
this.color=color;
System.out.println("颜色"+color);
}
//实现Face3接口
public void volume() {
System.out.println("圆柱体体积" + area() * height);
}
}
public class 例题8_12接口的继承 {
public static void main(String[] args) {
Cylinder volu=new Cylinder(3.0,2);
volu.setColor("红色");
volu.volume();
}
}