类的基本操作就不写上来了。
1.抽象类
用关键字abstract
package com.xxm.javaLange.test;
public class Abs3 {
public static void main(String[] args){
Rule1 abs1 = new Rule1(3,2);
Rule2 abs2 = new Rule2(4,2);
ClassAbstractRule abs;
abs = abs1;
System.out.println("Add is "+abs1.mathtext());
abs = abs2;
System.out.println("delete is "+abs2.mathtext());
}
}
abstract class ClassAbstractRule {
int a1;
int b1;
ClassAbstractRule(int a,int b){
a1 = a;
b1 = b;
}
abstract int mathtext();//抽象方法在子类中需要重写
}
class Rule1 extends ClassAbstractRule{
Rule1(int a,int b){
super(a,b);
}
int mathtext() {
return a1+b1;
}
}
class Rule2 extends ClassAbstractRule{
Rule2(int a,int b){
super(a,b);
}
int mathtext() {
return a1-b1;
}
}
结果:
Add is 5
delete is 2
2.接口
用关键字interface,默认是public(也只能是它)。
package com.xxm.javaLange.test;
public class ClassInterface {
public static void main(String[] args){
interfaceFather aa = new interfaceFather();
System.out.println("a+b="+aa.add(100));
System.out.println("a-b="+aa.sub(20, 100));
System.out.println("a*b="+aa.mul(20,100));
}
}
interface interfaceOne{
int c = 20;//接口中只能定义常量
int add(int b);
}
interface interfaceTwo{
int sub(int a,int b);
}
interface interfaceThree{
int mul(int a,int b);
}
class interfaceFather implements interfaceOne,interfaceTwo,interfaceThree{
public int add(int b){//实际接口方法时候,必须加上修饰public
return c+b;//c是接口interfaceOne的常量,可以直接使用
}
public int sub(int a,int b){
return a-b;
}
public int mul(int a,int b){
return a*b;
}
}
结果:
a+b=120
a-b=-80
a*b=2000
3.多次继承
package com.xxm.javaLange.test;
public class ClassMultInherit {
public static void main(String[] args){
Factory a1 = new Factory();
Factory a2 = new Factory("my city",92099,25, "万卷出版公司","大顺印刷厂");
Factory a3 = new Factory(a2);
System.out.println(a1.badder);
System.out.println(a1.factory);
a1.print();
System.out.println(a2.badder);
System.out.println(a2.factory);
a2.print();
a3.print();
}
}
class NewBook{
String bname;
int bid;
int bprice;
NewBook(){
bname = "小时代";
bid = 14002;
bprice = 29;
}
NewBook(NewBook a){
bname = a.bname;
bid = a.bid;
bprice = a.bprice;
}
NewBook(String name,int id,int price){
bname = name;
bid = id;
bprice = price;
}
void print(){
System.out.println("书名:"+bname+" 序号:"+bid+" 价格:"+bprice);
}
}
class BadderNew extends NewBook{
String badder;
BadderNew(){
super();
badder = "长江文艺出版社";
}
BadderNew(BadderNew b){
super(b);
badder = b.badder;
}
BadderNew(String x,int y,int z,String aa){
super(x,y,z);
badder = aa;
}
}
class Factory extends BadderNew{
String factory;
Factory(){
super();
factory = "伯伯印刷厂";
}
Factory(Factory c){
super(c);
factory = c.factory;
}
Factory(String x,int y,int z,String l,String n){
super(x,y,z,l);
factory =n;
}
}
结果:
长江文艺出版社
伯伯印刷厂
书名:小时代 序号:14002 价格:29
万卷出版公司
大顺印刷厂
书名:my city 序号:92099 价格:25
书名:my city 序号:92099 价格:25
4.重载
函数重载,一个类里面多次重写一个函数,但参数不同。
package com.xxm.javaLange.test;
public class ClassOverload {
public static void main(String[] args){
Huaming a1 = new Huaming();
a1.ename = "李艳";
a1.age = 25;
a1.print();
a1.print("于莎莎",20);
a1.print("董明",16,10025);
}
}
class Huaming{
String ename;
int age;
void print(){
System.out.println("姓名为:"+ename+" 年龄:"+age);
}
void print(String a, int b){
System.out.println("姓名为:"+a+" 年龄:"+b);
}
void print(String a,int b,int c){
System.out.println("姓名为:"+a+" 年龄:"+b+" ID号:"+c);
}
}
结果:
姓名为:李艳 年龄:25
姓名为:于莎莎 年龄:20
姓名为:董明 年龄:16 ID号:10025
5.重写
区别于重栽,子类重写了父类的函数。
package com.xxm.javaLange.test;
public class ClassRewrite {
public static void main(String[] args){
CompanyOne a1 = new CompanyOne("浙江工业大学",72221,7001,"杭州西湖区留和路");
a1.print();
}
}
class Company{
String sname;
int sid;
int snumber;
public void print(){
System.out.println("公司名:"+sname+" 序号:"+sid+" 公司人数:"+snumber);
}
Company(String name,int id,int number){
sname = name;
sid = id;
snumber = number;
}
}
class CompanyOne extends Company{
String sadder;
CompanyOne(String x,int y,int z,String aa){
super(x,y,z);
sadder = aa;
}
public void print(){//这里不能用private
System.out.println("公司全名为:"+sname+"序号:"+sid+"总人数:"+snumber+"公司地址"+sadder);
}
}
结果:
公司全名为:浙江工业大学序号:72221 总人数:7001 公司地址杭州西湖区留和路
6.内部类
类里面的类。(没搞明白,创造这个功能什么用)
package com.xxm.javaLange.test;
public class ClassInternal {
public static void main(String[] args){
class foo{
public String name;
public foo(String x){
name = x;
}
}
Object obj = new foo("hi");
foo p = (foo)obj;
System.out.println(p.name);
}
}
结果:
hi