面向对象 抽象(abstract)

面向对象 抽象(abstract)

抽象类的概述

  • A:抽象类概述
    • 抽象就是看不懂的
  • B:抽象类特点
    • a:抽象类和抽象方法必须用abstract关键字修饰
      • abstract class 类名 {}
      • public abstract void eat();
    • b:抽象类不一定有抽象方法,有抽象方法的类一定是抽象类或者是接口
    • c:抽象类不能实例化那么,抽象类如何实例化呢?
      • 按照多态的方式,由具体的子类实例化。其实这也是多态的一种,抽象类多态。
    • d:抽象类的子类
      • 要么是抽象类
      • 要么重写抽象类中的所有抽象方法
  • C:案例演示
    • 抽象类特点
抽象类成员特点
  • A:抽象类的成员特点
    • a:成员变量:既可以是变量,也可以是常量。abstract是否可以修饰成员变量?不能修饰成员变量
    • b:构造方法:有。
      • 用于子类访问父类数据的初始化。
    • c:成员方法:既可以是抽象的,也可以是非抽象的。
  • B:案例演示
    • 抽象类的成员特点
  • C:抽象类的成员方法特性:
    • a:抽象方法 强制要求子类做的事情。
    • b:非抽象方法 子类继承的事情,提高代码复用性。

抽象类 之 葵花宝典

public class Test_葵花宝典 {public static void main(String[] args) {岳不群  小岳子 = new 岳不群();小岳子.自宫();}}
abstract class 葵花宝典{  //可以了理解为 葵花宝典是抽象的武功
public abstract void 自宫();  //想要练成葵花宝典  必须自宫,由于自宫方法很多所以用到抽象方法
}class 岳不群 extends 葵花宝典{
public void 自宫(){System.out.println("用牙签");
}   
}class 林平之 extends 葵花宝典{
public void 自宫(){System.out.println("用指甲刀");
}
}

抽象类 之 猫狗

  • 具体事物:猫,狗

    • 共性:姓名,年龄,吃饭
    • 猫的特性:抓老鼠
    • 狗的特性:看家

    public class Text_2{

    public static void main(String[] args) {Cat c = new Cat("加菲",8);
    System.out.println(c.getName()+ "..." + c.getAge());
    c.eat();
    c.catchMousr();Dog d = new Dog("八公",30);
    System.out.println(d.getName() + "..." + d.getAge());
    d.eat();
    d.lookHome();
    }
    }abstract class Animal{
    private String name;
    private int age;public Animal(){}public Animal(String name,int age){this.name = name;this.age = age;
    }
    public void setName(String name){ //设置姓名this.name =name;
    }
    public String getName(){   // 获取姓名return name;
    }
    public void setAge(int age){ //设置年龄this.age = age;
    }
    public int getAge(){  //获取年龄return age;
    }
    public abstract void eat(); // 吃饭      由于不同的动物的吃饭方法不同,所以这里定义吃饭这个方法为abstract方法,因此,Animal类亦应该定义为abstract类
    }class Cat extends Animal{public Cat(){}public Cat(String name,int age){super(name,age);
    }public void eat(){                                  //猫的吃饭法方法System.out.println("猫吃鱼");
    }
    public void catchMousr(){                    //猫的抓老鼠方法System.out.println("抓老鼠");
    }class Dog extends Animal{             
    public Dog(){}
    public Dog(String name,int age){super(name,age);
    }public void eat(){                                         //狗的吃饭法方法System.out.println("狗吃肉");}
    public void lookHome(){                              //狗的看家方法
    System.out.println("看家");}
    

    }

抽象类 之 老师

  • 具体事物:基础班老师,就业班老师
    • 共性:姓名,年龄,讲课。
    • 具体事物:基础班学生,就业班学生
    • 共性:姓名,年龄,学习

public class Text_Teacher {

public static void main(String[] args) {BaseTeacher bt = new BaseTeacher("黄家驹",32);bt.teach();
}
}
abstract class Teacher {private String name;
private int age;public Teacher(){}  //空参构造public Teacher(String name,int age){  //有参构造this.name = name;this.age = age;
}public void setName(String name){ //设置姓名this.name = name;
}
public String getName(){  //获取姓名return name;
}public void setAge(int age){ // 设置年龄this.age = age;
}public int getAge(){ // 获取年龄return age;
}public abstract void teach();//由于不同类型的老师讲课的方式不同,所以这里的teach方法定义成abstract类型。由于这里的teach方法为抽象类型,所以这里的Teach类也得定义成abstract类。}class BaseTeacher extends Teacher{
public BaseTeacher(){}public BaseTeacher(String name,int age){super(name,age);
}public void teach(){System.out .println("我的姓名是:" + this.getName() + "   我的年龄是:" + this.getAge() + "  讲的内容是Java基础。");
}
}

抽象类 之 经理与员工

  • 假如我们在开发一个系统时需要对程序员类进行设计,程序员包含3个属性:姓名、工号以及工资。
    • 经理,除了含有程序员的属性外,另为还有一个奖金属性。
    • 请使用继承的思想设计出程序员类和经理类。要求类中提供必要的方法进行属性访问。
public class Test_Employee {public static void main(String[] args) {Coder c =new Coder("德玛西亚","007",8000);c.work();Manager m = new Manager("苍老师","9527",3000,20000);m.work();}}abstract class Employee{//先定义成员变量private String name;  //姓名private String id;    //工号private double salary;//工资public Employee(){}   //空参构造public Employee(String name,String id,double salary){this.name = name;this.id = id;this.salary = salary;}public void setName(String name){  //设置姓名this.name = name;}public String getName(){           //获取姓名return name;}public void setId (String id){     //设置idthis.id = id;}public String getId(){             //获取idreturn id;}public void setSalary(double salary){//设置工资this.salary =salary; }public double getSalary(){           //获取工资return salary;}public abstract void work();}//程序员class Coder extends Employee{public Coder(){}   //空参构造public Coder(String name,String id,double salary){super(name,id,salary);}public void work(){System.out.println("我的姓名是:"+ this.getName() + ",我的工号是"  + this.getId() + ",我的工资是:" + this.getSalary() + ",我的工作内容是敲代码");}   }//项目经理class Manager extends Employee{private int bonus;    //奖金public Manager (){}   //空参构造public Manager(String name,String id,double salary,int bonus){super(name,id,salary);this.bonus=bonus;}public void work(){System.out.println("我的姓名是:"+ this.getName() + ",我的工号是"  + this.getId() + ",我的工资是:" + this.getSalary() +"我的奖金是:"+bonus+ ",我的工作内容是管理");}   }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/380181.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

密码学加密算法分类_密码学中的国际数据加密算法(IDEA)

密码学加密算法分类Introduction 介绍 International Data Encryption Algorithm (IDEA) is a type of cryptography as a block cipher algorithm designed by "Xuejia Lai" and "James L.Massey" of ETH-Zrich and was first published in the 1991 yea…

如何定位溢出点位置

程序&#xff1a; #include <stdio.h> void exploit() {system("/bin/sh"); } void func() {char str[20];read(0,str,50);printf("the str is:%s\n",str); } int main() {func();return 0; }关掉保护机制&#xff1a; gcc -no-pie -fno-stack-pro…

.net知识和学习方法系列(六)关于数值类型

过年总是忙&#xff0c;没有更多的时间来写博客&#xff0c;不过还是挺想念这块地方的。 本次博客说一下数值类型吧&#xff01; 不管那种语言&#xff0c;都为数据类型一说&#xff0c;在C#中也是&#xff0c;不过C#的数据类型分了两类&#xff0c;一是值类型&#xff0c;一是…

【竞赛题解】2021年广东工业大学第十五届文远知行杯程序设计竞赛(同步赛)

B 找山坡 题意&#xff1a;在数组中找到两相等元素相距最大的距离且这两元素间的元素都不小于该两端值 思路&#xff1a;采用单调栈 例如&#xff1a;a[] { 2 3 5 4 6 3 }&#xff0c;栈内存储元素的坐标&#xff08;从1开始&#xff09;&#xff0c;便于计算距离 首先将a[…

[转]JAVA AES 加密算法

本文转自&#xff1a;http://blog.sina.com.cn/s/blog_7c8eb1590100svr0.html package com.siro.tools;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64E…

java中Scanner类中 next()与nextLine()的区别

问题&#xff1a;提示用户输入一个英文字符串或者要解密的字符串&#xff0c;然后通过扫描仪获取用户输入的字符串&#xff0c;经过加密或者解密后&#xff0c;把字符串输出。 import java.util.Scanner;public class Encryption {public static void main(String[] args) {Sca…

操作系统中的处理机调度调度_操作系统中的流程分类和调度

操作系统中的处理机调度调度处理 (Process) In the operating system, there are numerous task and application program run simultaneously. A program is stored in the hard disk or any other form of secondary storage. When the program is executed it must be loade…

NX机制及绕过策略-ret2libc

程序&#xff1a; 1.c #include <stdio.h> void exploit() {system("/bin/sh"); } void func() {char str[0x20];read(0,str,0x50); } int main() {func();return 0; }0x01 NX介绍 溢出攻击的本质在于冯诺依曼计算机模型对数据和代码没有明确区分这一先天性缺…

网站SEO策略的制定

在对一个网站做SEO的时候&#xff0c;SEO技术水平类似的人&#xff0c;营销效果可能天壤之别&#xff0c;这是因为网站SEO策略的制定的不同&#xff01;-----这个是最根本的。 SEO技术非常的简单&#xff0c;因为SEO不是去寻找搜索引擎的漏洞&#xff0c;而是根据搜索引…

Python | 程序从列表中删除范围内的所有元素

Given a list and we have to remove elements in a range from the list in Python. 给定一个列表&#xff0c;我们必须从Python中的列表中删除范围内的元素。 删除列表(开始索引&#xff0c;结束索引) (del list(start_index, end_index)) del() method is used to remove a…

面向对象 (接口 Interface)

1&#xff0c;面向对象(接口的概述及其特点) A:接口概述 从狭义的角度讲就是指java中的interface从广义的角度讲对外提供规则的都是接口 B:接口特点 a:接口用关键字interface表示 interface 接口名 {}b:类实现接口用implements表示 class 类名 implements 接口名 {}c:接口…

android unbound prefix

少一个命名空间加上就行了&#xff1a;xmlns:android"http://schemas.android.com/apk/res/android" 转载于:https://www.cnblogs.com/nizuimeiabc1/archive/2011/10/09/4254310.html

【竞赛题解】第22次CCF计算机软件能力认证 B

今天&#xff08;准确说是昨天&#xff0c;一下子就过12点了&#xff09;下午刚参加了CSP认证考试&#xff0c;大概是考了220&#xff08;前两题AC&#xff0c;第三题太折磨了懒得看了&#xff0c;后面两题各混了10分&#xff09;&#xff0c;唯一有点参与感的就是B题了&#x…

gbd调试64位程序关键

程序&#xff1a; 4.c&#xff1a; #include<stdio.h> void exploit() {system("/bin/sh"); } void main() {char buf[20];gets(buf); }编译&#xff1a; gcc -no-pie -fno-stack-protector -m64 -o 4.exe 4.cNX保护&#xff0c;栈数据不可执行 使用命令&…

C#全局鼠标键盘Hook (备查)

using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace DCIEngine.FrameWork.Snap { /// <summary> /// 这个类可以让你得到一个在…

fcfs调度算法_FCFS:先来先服务调度算法

fcfs调度算法The FCFS, which stands for First Come First Serve Scheduling Algorithm, is a non-preemptive scheduling algorithm, which means that if a process once starts executing in the processor, then it cannot be preempted in between the processing. Thus,…

亲和数

Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现&#xff0c;220的所有真约数(即不是自身的约数)之和为&#xff1a; 1245101120224455110&#xff1d;284。 1* 220220&#xff1b;2* 110220&#xff1b;4* 55220&#xff1b;5* 44220&#xff1b;10*20220;…

转:JNI jstring与c++字符串类型转换函数

jstring与c字符串类型转换函数 jstring str2jstring(JNIEnv* env,const char* pat) {//定义java String类 strClassjclass strClass (env)->FindClass("Ljava/lang/String;");//获取String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新Stringjmetho…

python字符串转浮点数_如何在Python中检查字符串是否为数字(浮点数)?

python字符串转浮点数Using python it is very to interconvert the datatypes of a variable. A string can be easily converted to an integer or a float. However, asserting a string to be a float is a task by itself. Python provides an option to assert if a stri…

nhibernate学习之三级联(Ternary Associations)篇

1) 学习目标通过进一步学习Nhibernate基础知识&#xff0c;掌握用Nhiberate实现对级联的支持&#xff0c;通过一个简单的用户角色权限系统来体验nhibernate对级联的强大支持。2&#xff09;开发环境和必要准备 开发环境为:windows 2003,Visual studio .Net 2005,Sql server 200…