美文网首页
面对对象程序(二)

面对对象程序(二)

作者: koalaUD | 来源:发表于2018-11-22 21:02 被阅读0次

面向对象三大特性:

1. 封装

封装是面向对象编程的核心思想。封装的载体是类,对象的属性和行为被封装在这个类中。
例子:

public class Fruit {
    //将水果的名称和价格封装起来
    private String fruitName;
    private String fruitPrice;
}

2.继承

概念:
子类继承父类,可以继承父类原有的属性和方法,也可以增加其他的属性和方法,可以直接重写父类中的某些方法。

例子1:

  • Human为父类
public class Human {
    public void eat(){
        System.out.println("人在吃饭");
    }
}

  • Student为子类
public class Student extends Human{
    @Override
    public void eat() {
        //调用父类的方法
        super.eat();
        System.out.println("学生在吃饭");
    }
}

  • 测试程序
public class StudentTest {
    public static void main(String[] args) {
        Human human = new Human();
        human.eat();
        Student student = new Student();
        student.eat();
    }
}

  • 运行结果

    [图片上传失败...(image-ce0214-1542891701684)]

例子2:
自定义组件:

  • 创建MyButton类,继承Button
import javafx.scene.control.Button;

/**
 * 自定义按钮
 */
public class MyButton extends Button {
    //自定义构造方法,实现一个指定宽和高和背景色的按钮
    public MyButton(){
        //给当前按钮对象设置合适的尺寸
       this.setPrefSize(100,35);
       //给当前按钮设置背景色、文本色、字体大小
       this.setStyle("-fx-background-color: rgb(19, 209, 190);-fx-font-size: 14px;-fx-text-fill: #FFFFFF");
    }
}

  • 使用这个按钮
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import com.soft1841.oop.week1.MyButton?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="com.soft1841.oop.week1.ButtonController">
    <MyButton text="美团"
              AnchorPane.leftAnchor="100"
              AnchorPane.topAnchor="100"/>
    <MyButton text="设置"
              AnchorPane.leftAnchor="260"
              AnchorPane.topAnchor="100"/>
</AnchorPane>

  • 主程序
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;

public class ButtonApp extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        URL location = getClass().getResource("/fxml/button.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader(location);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root,800,600);
        primaryStage.setTitle("自定义按钮");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

  • 运行效果
image.png

相关文章

  • 面对对象程序(二)

    面向对象三大特性: 1. 封装 封装是面向对象编程的核心思想。封装的载体是类,对象的属性和行为被封装在这个类中。例...

  • py面对对象编程及类和实例

    一、面对对象编程(object orientend programming) 一种程序设计思想:将程序分解为对象,...

  • 面对对象程序(一)

    类的封装 属性私有对外提供公有的getter/setter 1.类之间的关系 1.纵向关系:;比较清晰 继承关系:...

  • 微信小程序学习(二)

    小程序的js有些额外的成员 App 方法 用于定义应用程序实例对象 Page 方法 用于定义页面对象 getApp...

  • 面对对象

    什么是面对对象 面向对象程序设计(英语:Object-oriented programming,缩写:OOP)是种...

  • OC中类与对象的基础应用

    对象,是面对对象程序的核心面对对象三大特征:继承,封装,多态OC中的类:类可以是任意的事物,是具有相同特征的事物 ...

  • 设计模式---Builder模式

    1.什么是builder模式 简单解释,在程序设计的时候,如果面对的对象属性较多,对象复杂性比较大(例如对象包...

  • 外观模式

    门面模式意图 通过门面的包装,使应用程序只能看到门面对象,而不会看到具体的细节对象,这样无疑会降低应用程序的复杂度...

  • 使用页面对象组织e2e测试

    当我们编写e2e测试时,一个共同的模式是使用页面对象。页面对象通过封装应用程序页面上的元素信息帮助我们使测试变得简...

  • C++面向对象语言程序设计中的6大重点

    1.面向对象程序设计的基本思想是啥? 咱们先简单说说对象和类 对象:是人们要研究的任何事物。在面对对象软件系统中则...

网友评论

      本文标题:面对对象程序(二)

      本文链接:https://www.haomeiwen.com/subject/uwurqqtx.html