如何通过实例方法传递整数并将其与实例变量相加?

时间:2019-03-02 08:45:49

标签: python python-3.x class parameter-passing instance-variables

我试图在实例变量bonus上添加参数self.pay(将使用整数),并想用工人的名字打印最后一笔付款。但是,我无法打印出总付款额

我想调用方法rise()而不是从中返回任何内容,但是我很困惑如何调用它并传递一个整数。

class Information:
    def __init__(self,first,last,pay):

        self.first = first
        self.last = last
        self.pay = pay


    def rise(self,int(bonus)):
        self.pay = self.pay + bonus

    def __str__(self):
        return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)

if __name__ == "__main__":
    emp1 = Information("tom","jerry",999)
    print (emp1)

2 个答案:

答案 0 :(得分:1)

import 'package:flutter/material.dart';
import 'package:adminify/tabs/first.dart';
import 'package:adminify/tabs/second.dart';
import 'package:adminify/tabs/third.dart';
import 'package:adminify/tabs/fourth.dart';
import 'package:adminify/tabs/fifth.dart';

class NavigationIconView {
  NavigationIconView({
    Widget icon,
    Widget activeIcon,
    String title,
    Color color,
    TickerProvider vsync,
  }) : _icon = icon,

       _title = title,
       item = BottomNavigationBarItem(
         icon: icon,
         activeIcon: activeIcon,
         title: Text(title),
         backgroundColor: color,
       ),
       controller = AnimationController(
         duration: kThemeAnimationDuration,
         vsync: vsync,
       ) {
    _animation = controller.drive(CurveTween(
      curve: const Interval(0.5, 1.0, curve: Curves.fastOutSlowIn),
    ));
  }

  final Widget _icon;

  final String _title;
  final BottomNavigationBarItem item;
  final AnimationController controller;
  Animation<double> _animation;

  FadeTransition transition(BottomNavigationBarType type, BuildContext context) {
    Color iconColor;
      final ThemeData themeData = Theme.of(context);
      iconColor = themeData.brightness == Brightness.light
          ? themeData.primaryColor
          : themeData.accentColor;


    return FadeTransition(
      opacity: _animation,
      child: SlideTransition(
        position: _animation.drive(
          Tween<Offset>(
            begin: const Offset(0.0, 0.02), // Slightly down.
            end: Offset.zero,
          ),
        ),
        child: IconTheme(
          data: IconThemeData(
            color: iconColor,
            size: 120.0,
          ),
          child: Semantics(
            label: 'Placeholder for $_title tab',
            child: _icon,
          ),
        ),
      ),
    );
  }
}

class BottomNavigationDemo extends StatefulWidget {
  static const String routeName = '/material/bottom_navigation';

  @override
  _BottomNavigationDemoState createState() => _BottomNavigationDemoState();
}

class _BottomNavigationDemoState extends State<BottomNavigationDemo>
    with TickerProviderStateMixin {
  int _currentIndex = 0;
  BottomNavigationBarType _type = BottomNavigationBarType.fixed;
  List<NavigationIconView> _navigationViews;

  @override
  void initState() {
    super.initState();
    _navigationViews = <NavigationIconView>[
      NavigationIconView(
        icon: const Icon(Icons.home),
        title: 'Home',
        color: Colors.deepPurple,
        vsync: this,
      ),
      NavigationIconView(

        activeIcon: const Icon(Icons.favorite),
        icon: const Icon(Icons.favorite_border),
        title: 'Explore',
        color: Colors.deepOrange,
        vsync: this,
      ),
      NavigationIconView(
        activeIcon: const Icon(Icons.cloud),
        icon: const Icon(Icons.cloud_queue),
        title: 'Cloud',
        color: Colors.teal,
        vsync: this,
      ),
      NavigationIconView(
        activeIcon: const Icon(Icons.favorite),
        icon: const Icon(Icons.favorite_border),
        title: 'Saved',
        color: Colors.indigo,
        vsync: this,
      ),
      NavigationIconView(
        icon: const Icon(Icons.event_available),
        title: 'Profile',
        color: Colors.pink,
        vsync: this,
      )
    ];

    _navigationViews[_currentIndex].controller.value = 1.0;
  }

  @override
  void dispose() {   
    for (NavigationIconView view in _navigationViews)
      view.controller.dispose();
    super.dispose();

  }

  Widget _buildTransitionsStack() {
    final List<FadeTransition> transitions = <FadeTransition>[];

    for (NavigationIconView view in _navigationViews)
      transitions.add(view.transition(_type, context));

    // We want to have the newly animating (fading in) views on top.
    transitions.sort((FadeTransition a, FadeTransition b) {
      final Animation<double> aAnimation = a.opacity;
      final Animation<double> bAnimation = b.opacity;
      final double aValue = aAnimation.value;
      final double bValue = bAnimation.value;
      return aValue.compareTo(bValue);
    });

    return Stack(children: transitions);
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar botNavBar = BottomNavigationBar(
      items: _navigationViews
          .map<BottomNavigationBarItem>((NavigationIconView navigationView) => navigationView.item)
          .toList(),
      currentIndex: _currentIndex,
      type: _type,
      onTap: (int index) {
        setState(() {
          _navigationViews[_currentIndex].controller.reverse();
          _currentIndex = index;
          _navigationViews[_currentIndex].controller.forward();
        });
      },
    );

    return Scaffold(

      body: Center(
        child: _buildTransitionsStack()
      ),

      bottomNavigationBar: botNavBar,
    );
  }
}

答案 1 :(得分:0)

我尝试了以下代码。

我将防御力上升(自我,奖励):更新为防御力上升(自我,奖励):

class Information:
    def __init__(self,first,last,pay):

        self.first = first
        self.last = last
        self.pay = pay


    def rise(self,bonus):
        self.pay = self.pay + bonus

    def __str__(self):
        return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)

if __name__ == "__main__":
    emp1 = Information("tom","jerry",999)
    emp1.rise(89)
    print (emp1)